I was reading about the DP version of fibonnaci.
In Sedgewick I saw:
int[] T = new int[47];
for storage of the previous calculations. Elsewhere I saw that the max input for fibonacci should be less than 92
.
It is not clear to me how does these numbers come up? I understand that it has to do with overflow and size of int
but I am not clear how we end up with these limits.
Any help?

- 52,998
- 69
- 209
- 339
-
an `int` is a 32 bit signed two's complement number so the maximum size is 2^(32-1). – Boris the Spider Feb 25 '13 at 10:52
-
Did you try to implement this yourself? If so, take a look at the numbers and observe how big the numbers get. Then observe the size of `int` and `long`. Take a look at `BigDecimal` as well. – nwinkler Feb 25 '13 at 10:53
-
Just use unsigned long number. – Толя Feb 25 '13 at 11:22
5 Answers
There is a closed-form expression for the n-th Fibonacci number, Binet's formula,
F(n) = (φ^n - ψ^n) / (φ - ψ)
where
φ = (1 + √5)/2; ψ = 1 - φ = -1/φ
Now |ψ| < 1
, so the ψ^n
term converges to 0 pretty fast, hence in estimating the size of F(n)
it can be ignored except for the first few numbers.
So if you have an integer type with b
bits used for the representation of positive integers, you can represent the Fibonacci numbers with
F(n) < 2^b
(since the maximal number that can be represented is 2^b - 1
). Ignoring the ψ^n
term and using φ - ψ = √5
, we find the condition
φ^n < 2^b * √5
<=> n*log φ < b*log 2 + 1/2*log 5
<=> n < b*(log 2 / log φ) + 1/2*(log 5 / log φ)
log 2 / log φ ≈ 1.44042009
and 1/2*(log 5 / log φ) ≈ 1.672275938
, so with a signed 32-bit integer type (which has 31 bits to represent positive numbers, since one bit is used for the sign), you can represent the Fibonacci numbers for
n < 31*(log 2 / log φ) + 1/2*(log 5 / log φ) ≈ 44.65 + 1.67 ≈ 46.32
i.e. the 47 Fibonacci numbers with index between 0 and 46 (inclusive). With an unsigned 32-bit integer type you could also represent F(47)
.
With a signed 64-bit integer type, you can represent the Fibonacci numbers for
n < 63*(log 2 / log φ) + 1/2*(log 5 / log φ) ≈ 90.75 + 1.67 ≈ 92.42
and with an unsigned 64-bit integer type You can also represent F(93)
.

- 181,706
- 17
- 308
- 431
-
1+1.This answer seems scary.I will try to read it.Not sure if I will be able to understand it ;) – Cratylus Feb 25 '13 at 18:00
Well, the fibonacci series grows (approximately) exponentially with a ratio of 1.618 (the golden ratio).
If you take the log base 1.618 of Integer.MAX_VALUE
it will therefore tell you approximately how many iterations you can go before overflowing....
Alternatively, you can determine empirically when it overflows just by doing the calculations....

- 105,238
- 25
- 256
- 415
-
This covers `<47`.What about `<92`?http://introcs.cs.princeton.edu/java/96optimization/Fibonacci.java.html – Cratylus Feb 25 '13 at 10:56
-
-
You can just build the sequence and see when it cross the limits for signed and unsigned `int` using 4 or 8 bytes and get the numbers – arfneto Sep 14 '20 at 14:07
-
u can manually patch multiple processor registers together and use the overflow-flag to calculate n * 64 bit large numbers. i did this for an old 8080 [with 8bit registers] to implement a DOUBLEWORD – clockw0rk Jan 04 '21 at 13:31
(signed) int
has a value range of −2.147.483.648 ... 2.147.483.647
, so storing a fibonanacci number larger than 2.147.483.647 does not work.
The question now is: What is the first fibonnacci number larger than that value?
Spreadsheet says:
n fib(n)
1 0
2 1
3 1
4 2
5 3
6 5
7 8
8 13
9 21
10 34
11 55
12 89
13 144
14 233
15 377
16 610
17 987
18 1597
19 2584
20 4181
21 6765
22 10946
23 17711
24 28657
25 46368
26 75025
27 121393
28 196418
29 317811
30 514229
31 832040
32 1346269
33 2178309
34 3524578
35 5702887
36 9227465
37 14930352
38 24157817
39 39088169
40 63245986
41 102334155
42 165580141
43 267914296
44 433494437
45 701408733
46 1134903170
47 1836311903
48 2971215073
49 4807526976
So you can see: fibonnacci numbers after #47 won't fit in a (signed) int
.
To clarify: Unlike C Java does not have unsigned
types. So the emphasis on signed int
is kind of obsolete.

- 1,640
- 13
- 19
-
2you table incorrect f(1) =1 , f(2) =1. Also you foget about array enumerated from 0, in array with size 47 last index 46. – Толя Feb 25 '13 at 10:54
-
well, this depends on the definition. i've seen both `f(1) = 1` and `f(0) = 1`. – bidifx Feb 25 '13 at 10:59
-
But not everyone who reads "algorithm" tag know Java, so the emphasis on `signed int` was right in its place. :) – Will Ness Feb 25 '13 at 11:39
-
1
-
1Java had always had an un signed type: **char** which is an **unsigned** 16bit integer. – MrSmith42 Sep 15 '19 at 12:37
You may use following formula:
F(2n) = F(n)* (2*F(n-1) + F(n))
n=46
F(92) = F(46) * (2*F(45) +F(46))
This is a Matrix Form of Fibonacci.
Full list of number (ulong not overflowed)
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
17 1597
18 2584
19 4181
20 6765
21 10946
22 17711
23 28657
24 46368
25 75025
26 121393
27 196418
28 317811
29 514229
30 832040
31 1346269
32 2178309
33 3524578
34 5702887
35 9227465
36 14930352
37 24157817
38 39088169
39 63245986
40 102334155
41 165580141
42 267914296
43 433494437
44 701408733
45 1134903170
46 1836311903
47 2971215073
48 4807526976
49 7778742049
50 12586269025
51 20365011074
52 32951280099
53 53316291173
54 86267571272
55 139583862445
56 225851433717
57 365435296162
58 591286729879
59 956722026041
60 1548008755920
61 2504730781961
62 4052739537881
63 6557470319842
64 10610209857723
65 17167680177565
66 27777890035288
67 44945570212853
68 72723460248141
69 117669030460994
70 190392490709135
71 308061521170129
72 498454011879264
73 806515533049393
74 1304969544928657
75 2111485077978050
76 3416454622906707
77 5527939700884757
78 8944394323791464
79 14472334024676221
80 23416728348467685
81 37889062373143906
82 61305790721611591
83 99194853094755497
84 160500643816367088
85 259695496911122585
86 420196140727489673
87 679891637638612258
88 1100087778366101931
89 1779979416004714189
90 2880067194370816120
91 4660046610375530309
92 7540113804746346429
As we see
45 1134903170
46 1836311903
92 7540113804746346429
7540113804746346429 = 1836311903*(2*1134903170 + 1836311903)

- 2,839
- 15
- 23
-
-
Click to provided link. Read paragraph "Matrix form" to understand proof of formula. THis is common formula, just use n=46. – Толя Feb 25 '13 at 12:14
First, is already done some classes for representing large numbers beyond the limits for some primitive type, like with some C modifier of the type long long. To see the largest term look on largest known Fibonacci number.

- 3,113
- 1
- 21
- 31