-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci.java
337 lines (291 loc) · 9.3 KB
/
Fibonacci.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.longluo.algorithm;
/**
* Fibonacci Algorithms
*
* @author longluo
* @date 2021-7-23
*/
public class Fibonacci {
/**
* The Fibonacci Recursive Method
*
* @param n
* @return the nth fibonacci number
*/
public static long fib_recursive(int n) {
if (n <= 1) {
return n;
}
return fib_recursive(n - 1) + fib_recursive(n - 2);
}
/**
* @param time
* @return
*/
public static int recursion_time(long time) {
long startTime = System.currentTimeMillis();
long endTime = 0;
int i = 2;
while (endTime < startTime + time * 1000) {
endTime = System.currentTimeMillis();
i++;
fib_recursive(i);
}
return i;
}
/**
* 测试递归法在1,5,10,50秒内使用迭代法算出的最大斐波那契数是第几个
*/
public static void time_recursion_test() {
long t1 = System.currentTimeMillis();
long t2 = 0;
int i = 3;
for (; t2 - t1 > 60000; ) {
fib_recursive(i);
i++;
t2 = System.currentTimeMillis();
if (t2 - t1 == 1000) {
System.out.println("1秒内最大斐波那契数是第:" + i + "个 ");
}
if (t2 - t1 == 5000) {
System.out.println("5秒内最大斐波那契数是第:" + i + "个 ");
}
if (t2 - t1 == 10000) {
System.out.println("10秒内最大斐波那契数是第:" + i + "个 ");
}
if (t2 - t1 == 50000) {
System.out.println("50秒内最大斐波那契数是第:" + i + "个 ");
}
}
}
/**
* 递归法在1,5,10,50秒内算出的最大斐波那契数是第几个
*/
public static void fib_recursion_time() {
System.out.println("The Max Fibonacci Number in 1 second is the " + recursion_time(1));
System.out.println("The Max Fibonacci Number in 5 second is the " + recursion_time(5));
System.out.println("The Max Fibonacci Number in 10 second is the " + recursion_time(10));
System.out.println("The Max Fibonacci Number in 50 second is the " + recursion_time(50));
}
/**
* The Dynamic Programming Method To Calculate the Fibonacci Numbers
* <p>
* The Iteration Method
*
* @param n
* @return
*/
public static int fib_dp(int n) {
/* Declare an array to store Fibonacci numbers. */
int f[] = new int[n + 2]; // 1 extra to handle case, n = 0
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
/* Add the previous 2 numbers in the series and store it */
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}
/**
* 用迭代法寻找编程环境支持的最大整数(int型)的斐波那契数是第几个斐波那契数
*
* @return
*/
public static int max_int_iteration() {
int p = 1;
int q = 1;
int r = 2;
int count = 3;
/* 一旦c达到编程环境最大斐波那契数,便会产生内存溢出,从而变成一个负数,到此循环结束 */
for (; q < r; ) {
p = q;
q = r;
r = p + q;
count++;
}
return count;
}
/**
* 用迭代法寻找编程环境支持的最大整数(long型)的斐波那契数是第几个斐波那契数
*
* @return
*/
public static long max_long_iteration() {
long a = 1, b = 1, c = 2;
long count = 3;
/* 一旦c达到编程环境最大斐波那契数,便会产生内存溢出,从而变成一个负数,到此循环结束 */
for (; b < c; ) {
a = b;
b = c;
c = a + b;
count++;
}
return count;
}
/**
* 在1,5,10,50秒内使用迭代法算出的最大斐波那契数是第几个
*/
public static void time_iteration() {
long t1 = System.currentTimeMillis();
long t2 = System.currentTimeMillis();
int a = 1;
int b = 1;
int c = 2;
long a1 = 0, a2 = 0, a3 = 0, a4 = 0;
long count = 3;
for (; t2 - t1 < 60000; ) {
a = b;
b = c;
c = a + b;
count++;
t2 = System.currentTimeMillis();
if (t2 - t1 == 1000) {
a1 = count;
}
if (t2 - t1 == 5000) {
a2 = count;
}
if (t2 - t1 == 10000) {
a3 = count;
}
if (t2 - t1 == 50000) {
a4 = count;
}
}
System.out.println("迭代法1秒内最大斐波那契数是第:" + a1 + "个 ");
System.out.println("迭代法5秒内最大斐波那契数是第:" + a2 + "个 ");
System.out.println("迭代法10秒内最大斐波那契数是第:" + a3 + "个 ");
System.out.println("迭代法50秒内最大斐波那契数是第:" + a4 + "个 ");
}
/**
* @param n
* @return
*/
public static int fib_4(int n) {
int F[][] = new int[][]{{1, 1}, {1, 0}};
if (n == 0) {
return 0;
}
power(F, n - 1);
return F[0][0];
}
/**
* Helper function that multiplies 2 matrices F and M of size 2*2,
* and puts the multiplication result back to F[][]
*
* @param F
* @param M
*/
public static void multiply(int F[][], int M[][]) {
int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
/**
* Helper function that calculates F[][] raise to the power n and puts the result in F[][]
* Note that this function is designed only for fib() and won't work as general power function
*
* @param F
* @param n
*/
public static void power(int F[][], int n) {
int i;
int M[][] = new int[][]{{1, 1}, {1, 0}};
// n - 1 times multiply the matrix to {{1,0},{0,1}}
for (i = 2; i <= n; i++) {
multiply(F, M);
}
}
public static int MAX = 1000;
public static int f[];
// Returns n'th fibonacci number using
// table f[]
public static int fib_7(int n) {
if (n == 0) {
return 0;
}
if (n == 1 || n == 2) {
return (f[n] = 1);
}
// If fib(n) is already computed
if (f[n] != 0) {
return f[n];
}
int k = (n & 1) == 1 ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1 if n is odd, else 0.
f[n] = (n & 1) == 1 ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) : (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
// Initialize array of dp
public static int[] dp = new int[10];
public static int fib(int n) {
if (n <= 1) {
return n;
}
// Temporary variables to store values of fib(n-1) & fib(n-2)
int first, second;
if (dp[n - 1] != -1) {
first = dp[n - 1];
} else {
first = fib(n - 1);
}
if (dp[n - 2] != -1) {
second = dp[n - 2];
} else {
second = fib(n - 2);
}
// Memoization
return dp[n] = first + second;
}
/**
*
* @param n
* @return
*/
public static int fib_new(int n) {
int result = 0;
if (n == 0) {
result = 0;
} else if (n == 1 || n == 2) {
result = 1;
} else if (n == 3) {
result = 2;
} else if (n >= 4) {
int divisor = n / 4;
int remainer = n % 4;
int a = fib_new(divisor);
int b = fib_new((divisor + 1));
int c = fib_new((divisor - 1));
int d = fib_new((divisor + 2));
if (remainer == 0) {
result = (int) ((Math.pow(b, 2) - Math.pow(c, 2)) * (Math.pow(c, 2) + 2 * Math.pow(a, 2) + Math.pow(b, 2)));
}
if (remainer == 1) {
result = (int) (Math.pow((Math.pow(b, 2) - Math.pow(c, 2)), 2) + Math.pow((Math.pow(a, 2) + Math.pow(b, 2)), 2));
}
if (remainer == 2) {
result = (int) ((Math.pow(a, 2) + Math.pow(b, 2)) * (3 * Math.pow(b, 2) + Math.pow(a, 2) - 2 * Math.pow(c, 2)));
}
if (remainer == 3) {
result = (int) (Math.pow((Math.pow(a, 2) + Math.pow(b, 2)), 2) + Math.pow((Math.pow(d, 2) - Math.pow(a, 2)), 2));
}
}
return result;
}
public static void main(String[] args) {
System.out.println(max_int_iteration());
System.out.println(max_long_iteration());
long t1 = System.currentTimeMillis();
long result = fib_recursive(47);
long t2 = System.currentTimeMillis();
System.out.println("递归法求斐波那契数:" + result);
System.out.println("递归算法Time is: " + (t2 - t1) / 1000.0 + " second");
}
}