Fibonacci Sequence in C generating negatives?

后端 未结 3 1247
借酒劲吻你
借酒劲吻你 2020-12-12 00:28

I\'m new to programming and need help in C. I am writing a program to generate a Fibonacci sequence for values with up to 1000 digits.

Here is my code:



        
3条回答
  •  Happy的楠姐
    2020-12-12 01:11

    The comment I posted above has the simple answer, but here's a more complete version: C often represents integers with a sequence of 32 bits, and the range of values they can take on are from -2,147,483,648 to 2,147,483,647.

    Notice what the 47th Fibonacci number is? 2,971,215,073

    After they overflow, they wrap around to the smallest integer possible; see 2's complement notation for more information!

    For a solution, I might suggest a BigInteger structure. But Fibonacci numbers get huge really fast, so I'm not sure you'd really want to calculate that many.

提交回复
热议问题