@@ -12,7 +12,7 @@ public class FibonacciSequence {
1212 private static final double PHI = (1 + Math .sqrt (5 )) / 2 ; // Golden ratio
1313
1414 public static final long fibonacciSequenceUsingLoop (int n ) {
15- long [] array = new long [n + 1 ];
15+ final long [] array = new long [n + 1 ];
1616 int counter = 0 ;
1717 while (counter <= n ) {
1818 long r = 0 ;
@@ -21,22 +21,61 @@ public static final long fibonacciSequenceUsingLoop(int n) {
2121 } else if (counter == 1 ) {
2222 r = 1 ;
2323 }
24+ // If r goes below zero then we have run out of bits in the long
25+ if (r < 0 )
26+ throw new IllegalArgumentException ("Run out of bits in long, n=" +n );
2427 array [counter ] = r ;
2528 counter ++;
2629 }
2730
2831 return array [n ];
2932 }
3033
34+ /**
35+ * Recursion with memoization
36+ */
3137 public static final long fibonacciSequenceUsingRecursion (int n ) {
32- if (n == 0 || n == 1 ) return n ;
33- return fibonacciSequenceUsingRecursion (n - 1 ) + fibonacciSequenceUsingRecursion (n - 2 );
38+ // Using the array to store values already computed
39+ final long [] array = new long [n + 1 ];
40+ return fibonacciSequenceUsingRecursion (array ,n );
41+ }
42+
43+ private static final long fibonacciSequenceUsingRecursion (long [] array , int n ) {
44+ if (n == 0 || n == 1 )
45+ return n ;
46+
47+ // If array already has a value then it has previously been computed
48+ if (array [n ] != 0 )
49+ return array [n ];
50+
51+ final String exception = "Run out of bits in long, n=" +n ;
52+
53+ final long r1 = fibonacciSequenceUsingRecursion (array , (n - 1 ));
54+ array [n -1 ] = r1 ; // memoization
55+ // If r1 goes below zero then we have run out of bits in the long
56+ if (r1 < 0 )
57+ throw new IllegalArgumentException (exception );
58+
59+ final long r2 = fibonacciSequenceUsingRecursion (array , (n - 2 ));
60+ array [n -2 ] = r2 ; // memoization
61+ // If r2 goes below zero then we have run out of bits in the long
62+ if (r2 < 0 )
63+ throw new IllegalArgumentException (exception );
64+
65+ final long r = r1 + r2 ;
66+ // If r goes below zero then we have run out of bits in the long
67+ if (r < 0 )
68+ throw new IllegalArgumentException ("Run out of bits in long, n=" +n );
69+
70+ array [n ] = r ; // memoization
71+
72+ return r ;
3473 }
3574
3675 public static final long fibonacciSequenceUsingMatrixMultiplication (int n ) {
3776 // m = [ 1 , 1 ]
38- // [ 1 , 0 ]
39- long [][] matrix = new long [2 ][2 ];
77+ // [ 1 , 0 ]
78+ final long [][] matrix = new long [2 ][2 ];
4079 matrix [0 ][0 ] = 1 ;
4180 matrix [0 ][1 ] = 1 ;
4281 matrix [1 ][0 ] = 1 ;
@@ -52,23 +91,26 @@ public static final long fibonacciSequenceUsingMatrixMultiplication(int n) {
5291 while (counter > 0 ) {
5392 temp = multiplyMatrices (matrix , temp );
5493 // Subtract an additional 1 the first time in the loop because the
55- // first multiplication is
56- // actually n -= 2 since it multiplying two matrices
94+ // first multiplication is actually n -= 2 since it multiplying two matrices
5795 counter -= (counter == n ) ? 2 : 1 ;
5896 }
59- return temp [0 ][1 ];
97+ final long r = temp [0 ][1 ];
98+ // If r goes below zero then we have run out of bits in the long
99+ if (r < 0 )
100+ throw new IllegalArgumentException ("Run out of bits in long, n=" +n );
101+ return r ;
60102 }
61103
62104 private static final long [][] multiplyMatrices (long [][] A , long [][] B ) {
63- long a = A [0 ][0 ];
64- long b = A [0 ][1 ];
65- long c = A [1 ][0 ];
66- long d = A [1 ][1 ];
105+ final long a = A [0 ][0 ];
106+ final long b = A [0 ][1 ];
107+ final long c = A [1 ][0 ];
108+ final long d = A [1 ][1 ];
67109
68- long e = B [0 ][0 ];
69- long f = B [0 ][1 ];
70- long g = B [1 ][0 ];
71- long h = B [1 ][1 ];
110+ final long e = B [0 ][0 ];
111+ final long f = B [0 ][1 ];
112+ final long g = B [1 ][0 ];
113+ final long h = B [1 ][1 ];
72114
73115 B [0 ][0 ] = a * e + b * g ;
74116 B [0 ][1 ] = a * f + b * h ;
@@ -79,6 +121,10 @@ private static final long[][] multiplyMatrices(long[][] A, long[][] B) {
79121 }
80122
81123 public static final long fibonacciSequenceUsingBinetsFormula (int n ) {
82- return (long ) Math .floor (Math .pow (PHI , n ) * INVERSE_SQUARE_ROOT_OF_5 + 0.5 );
124+ final long r = (long ) Math .floor (Math .pow (PHI , n ) * INVERSE_SQUARE_ROOT_OF_5 + 0.5 );
125+ // If r hits max value then we have run out of bits in the long
126+ if (r == Long .MAX_VALUE )
127+ throw new IllegalArgumentException ("Run out of bits in long, n=" +n );
128+ return r ;
83129 }
84130}
0 commit comments