斐波纳契数字在一定期限后变为负数 [英] Fibonacci numbers becoming negative after a certain term

查看:241
本文介绍了斐波纳契数字在一定期限后变为负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  program fibonacci $ b 

我在Fortran中编写了这个程序来显示斐波那契数列, $ b隐式无
整数:: x,p,c,i,t!初始化限制,上一个,当前,迭代和临时
print *,列出第一个x斐波那契数字:
read *,x!读取限制
p = 0!设置为零前
c = 1!将当前设置为1
do i = 1,x
print *,c !打印当前的斐波纳契数字
t = c!将临时变量设置为当前的
c = c + p!将当前的当前值加上前一个
p = t!将之前的值设置为临时值
end do!迭代直到达到限制'x'
结束程序fibonacci

当我编译并运行它,并输入数字10时,它的确如预期的那样

 列出第一个x斐波那契数字:
10
1
1
2
3
5
8
13
21
34
55

但是当我输入50时:

 列出第一个x斐波纳契数字:
50
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
-1323752223
512559680
-811192543
-298632863

我不知道问题出在哪里,因为就我所见,这个逻辑很完美。我的错误在哪里?

我使用gfortran编译器。



显然你使用的是一个四字节的整数( http://www-classes.usc.edu/engr/ce/108/text/ fbk01.htm )。之后,1836311903你超过了最大整数值(2147483647),并且计算溢出了。

您有两种方法可以更精确地计算斐波纳契数。首先,您可以找到一个支持8或16字节整数的系统/ Fortran编译器组合。至少在gfortran支持,似乎由系统特定的。另一种选择是使用多精度库,例如 gmp


I wrote this program in Fortran to display the Fibonacci numbers up to the x-th term:

program fibonacci
implicit none
integer :: x,p,c,i,t !initializes limit, previous, current, iterative, and temp
print *, "List the first x fibonacci numbers: "
read *, x      !reads the limit
p=0            !sets previous to zero
c=1            !sets current to 1
do i=1,x
    print *, c !prints the current fibonacci number
t = c      !sets the temporary variable to the current
c = c + p  !sets the current to the current plus the previous
p = t      !sets the previous to the temporary value
end do         !iterates until it reaches the limit 'x'
end program fibonacci

When I compile and run it, and enter the number 10, it does as is expected

 List the first x fibonacci numbers: 
10
       1
       1
       2
       3
       5
       8
      13
      21
      34
      55

but when I entered 50:

 List the first x fibonacci numbers: 
50
       1
       1
       2
       3
       5
       8
      13
      21
      34
      55
      89
     144
     233
     377
     610
     987
    1597
    2584
    4181
    6765
   10946
   17711
   28657
   46368
   75025
  121393
  196418
  317811
  514229
  832040
 1346269
 2178309
 3524578
 5702887
 9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
-1323752223
512559680
-811192543
-298632863

I don't know what the problem is, as far as I can see, the logic is sound. Where is my mistake?

I'm using the gfortran compiler.

解决方案

I'm no fortran expert, but I did take a class once...

Apparently you're using a four byte integer (http://www-classes.usc.edu/engr/ce/108/text/fbk01.htm) for these. After, 1836311903 you're exceeding the maximum integer value (2147483647) and the calculations are overflowing.

You have two options for calculating fibonacci numbers with more precision. First, you can find a system/fortran compiler combination that support 8 or 16 byte integers. Support in gfortran at least, seems to by system specific. Another option is to use a multiple precision library like gmp.

这篇关于斐波纳契数字在一定期限后变为负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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