使用 for 循环为斐波那契系列打印值,它将打印最多 47 个以上的值,显示错误 [英] using for loop for fibonacci series to print values it will print up to 47 values above that it shows error

查看:30
本文介绍了使用 for 循环为斐波那契系列打印值,它将打印最多 47 个以上的值,显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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)")

        }

我想打印斐波那契数列.编号应该是用户应该选择的任何一个.我的代码在上面我必须选择 num 作为 100 但它最多会打印 47 个值.这里打印的最终值是1836311903,最多不会打印100,显示错误.我如何找到数字 100 的斐波那契数列.

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.

推荐答案

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

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

2,971,215,073 大于 231 - 1 ... 这是最大的 32 位有符号整数.

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.)

我如何找到数字 100 的斐波那契数列.

How i find the fibonacci series for number 100.

您不能使用简单的整数算术.您将需要使用 Java 的 BigInteger 类的 Swift 等效项.

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

这篇关于使用 for 循环为斐波那契系列打印值,它将打印最多 47 个以上的值,显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆