-7
 var num = "100"
 var num = text2.text.toInt()
        var temp = 0
        var temp2 = 1
        if (nu == 1) {
            println(1)
        }
        else {
            for var valued = 2; valued<num;++valued {
            var temp3 = temp + temp2
            temp = temp2
            temp2 = temp3
            println("\(temp3)")

        }

I want to print the fibonacci series of number. The number should be any one that should be choose by user. My code is above i have to choose num as 100 but it will print up to 47 values. The final value print here is 1836311903. It will not print up to 100. It shows an error. How i find the fibonacci series for number 100.

1 Answers1

2

Fib(47) is 2,971,215,073.

2,971,215,073 is larger than 231 - 1 ... which is the largest 32 bit signed integer.

Hence your calculation is overflowing. (In a lot of programming languages, you wouldn't have gotten an error. You would have just gotten the wrong answer.)

How i find the fibonacci series for number 100.

You can't use simple integer arithmetic. You will need to use the Swift equivalent of Java's BigInteger class.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216