0
var arrF = [1]

    var arrS = [1,2]

    var numbers = [1]

    let last = numbers.last

    var x = 1
    var equals = [1]

    while arrF.count < 63 {

        numbers.append((last! + 1))

        arrF.append(numbers.reduce(1, combine: *))

    }

    for (index, value) in arrF.enumerate() {

        arrF[index] = Int(sqrt(Double(value)))
    }

    while arrS.count < 63 {

        arrS.append(arrS.reduce(0, combine: +))

    }

    while x < 63 {

        let a = arrF[x]

        let b = arrS[x]

        if a == b {

            equals.append(a * a)
        }

        x++
    }

    print(equals)

The code above searches for factorial numbers where the square root equals the sum of its original factors. It works fine, but if I make the while loops go on after 63 I get an "Dab Instruction error" in the reduce function line. Does anyone know how to fix this, has the reduce function a restriction?

  • you are basically calculating `2^64` - do you know how large that number is? It does not fit in a 64 bit integer. – luk2302 Feb 21 '16 at 12:53
  • @luk2302 The value is exactly the maximum `UInt64` value (all `1`s), plus one. So you'd need a *`65`* bit integer to store that. – Clashsoft Feb 21 '16 at 13:01
  • 64! is a number with 90 decimal digits. Is this from some programming challenge? In that case I assume that a "brute-force" attack does not work and you'll have to figure out a more efficient algorithm. – Martin R Feb 21 '16 at 13:02
  • @luk2302: I think your original comment was correct: The above code tries to calculate the factorial of 64. – Martin R Feb 21 '16 at 13:05
  • @MartinR yes, but the point at which it crashes is the calculation of 2^64 storing it inside a signed 64 bit integer. Therefore I changed it. – luk2302 Feb 21 '16 at 13:06
  • But it actually seems like OP does not want to calculate 2^64 but has a buggy implementation on top of the "too big number" problem. – luk2302 Feb 21 '16 at 13:07
  • Thanks for your comments! I found a new algorithm which would go to 2^90 if I would use this methode. (something with square roots of factors and that kind of things) – Luca Kok Feb 25 '16 at 19:01

0 Answers0