Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

The greatest performance improvement can be achieved by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991https://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

The greatest performance improvement can be achieved by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

The greatest performance improvement can be achieved by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also https://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

deleted 133 characters in body
Source Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92

It would be easier to store the least significant digit at index 0 of the arrays. This simply means that you remove all reverse() calls in ``infMult()andinfMult()intToIntArray()` and intToIntArray().

Remark: I would have expected some The greatest performance improvement can be achieved by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

However,This reduces the improvement was almost not measurable (0.01 instead ofcomputation time to 0.0110002 seconds). Exponentiation by repeated squaring probably pays off only for larger numbers and exponents.

It would be easier to store the least significant digit at index 0 of the arrays. This simply means that you remove all reverse() calls in ``infMult()andintToIntArray()`.

Remark: I would have expected some performance improvement by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

However, the improvement was almost not measurable (0.01 instead of 0.011 seconds). Exponentiation by repeated squaring probably pays off only for larger numbers and exponents.

It would be easier to store the least significant digit at index 0 of the arrays. This simply means that you remove all reverse() calls in infMult() and intToIntArray().

The greatest performance improvement can be achieved by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

This reduces the computation time to 0.0002 seconds.

added 64 characters in body
Source Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92
func digitSum(var x : Int) -> Int {
    var result = 0
    while x > 0 {
        result += x % 10
        x /= 10
    }
    return result
}

func powerDigitSum(x:Int, power:Int) -> Int {
    let powerSum = infPow(x, power)
    
    let result = reduce(powerSum, 0) { $0 + digitSum($1) }
    
    return result
}

Remark: I would have expected a greatsome performance improvement by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

However, the performance improvement was almost not significantmeasurable (0.01 instead of 0.011 seconds). Exponentiation by repeated squaring probably pays off only for larger numbers and exponents.

func digitSum(var x : Int) -> Int {
    var result = 0
    while x > 0 {
        result += x % 10
        x /= 10
    }
    return result
}

func powerDigitSum(x:Int, power:Int) -> Int {
    let powerSum = infPow(x, power)
    
    let result = reduce(powerSum, 0) { $0 + digitSum($1) }
    
    return result
}

Remark: I would have expected a great performance improvement by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

However, the performance improvement was not significant (0.01 instead of 0.011 seconds).

func digitSum(var x : Int) -> Int {
    var result = 0
    while x > 0 {
        result += x % 10
        x /= 10
    }
    return result
}

func powerDigitSum(x:Int, power:Int) -> Int {
    let powerSum = infPow(x, power)
    let result = reduce(powerSum, 0) { $0 + digitSum($1) }
    return result
}

Remark: I would have expected some performance improvement by using a better exponentiation algorithm in infPow(), such as Exponentiation by squaring (see also http://codereview.stackexchange.com/a/70197/35991 for a nice explanation).

However, the improvement was almost not measurable (0.01 instead of 0.011 seconds). Exponentiation by repeated squaring probably pays off only for larger numbers and exponents.

added 93 characters in body
Source Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92
Loading
Source Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92
Loading