As I am currently learning C#, I made a small program to calculate X amount of fibonacci numbers. However, as the numbers quickly get quite large, not even an unsigned long can hold the numbers. How do I solve this problem? Make my own superlarge integer datatype?
Asked
Active
Viewed 1,147 times
1
-
2Answered here http://stackoverflow.com/questions/9362703/fibonacci-datatype – Stig Hausberg Feb 28 '12 at 11:36
-
1Use [Int64](http://msdn.microsoft.com/en-us/library/6yy583ek.aspx)? Max value is 9,223,372,036,854,775,807, sounds reasonable! – Shai Feb 28 '12 at 11:37
-
@StigHausberg: Sorry, I didn't see that. – cgt Feb 28 '12 at 11:40
-
1@Shai: Even a `UInt64` can only hold the first 94 fibonacci numbers. – LukeH Feb 28 '12 at 12:07
3 Answers
5
Are you using .NET4 or newer? If so you could use BigInteger
.

LukeH
- 263,068
- 57
- 365
- 409
-
Thank you. BigInteger works great. Will mark this as the answer when the timer allows me to. – cgt Feb 28 '12 at 11:48
2
No data type will be able to hold all the numbers. There are too many.
The solvability of your problem depends critically on the upper bound you put on X. Depending on which it is, BigInteger
can be a possibility, as LukeH says.

Community
- 1
- 1

Daniel Daranas
- 22,454
- 9
- 63
- 116
-
2Obviously it cannot store _all_ the numbers as the sequence is infinite. All I want is a lot of them. :D – cgt Feb 28 '12 at 11:40
-
1The Fibonnaci numbers form an infinite countable set, there can never be 'too many'! :D – Shai Feb 28 '12 at 11:40
2
you can do String
sum processing for a calculate too many fib number. also can use BigInteger
for limited number

Nikson Kanti Paul
- 3,394
- 1
- 35
- 51