-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion25.java
More file actions
113 lines (102 loc) · 2.36 KB
/
Question25.java
File metadata and controls
113 lines (102 loc) · 2.36 KB
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
import java.math.*;
import java.util.*;
/**
* The Fibonacci sequence is defined by the recurrence relation:
*
* Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
*
* Hence the first 12 terms will be:
*
* F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55
* F11 = 89 F12 = 144
*
* The 12th term, F12, is the first term to contain three digits.
*
* What is the first term in the Fibonacci sequence to contain 1000 digits?
*
* @author chz
*
*/
public class Question25 {
static int curr[] = new int[1024];
static int prev[] = new int[1024];
static int pos = 0;
static int term = 0;
private static void init() {
curr[0] = 1;
prev[0] = 1;
pos = 1;
term = 2;
}
private static void getNextFeb() {
// swap(curr, prev)
int[] tmp = curr;
curr = prev;
prev = tmp;
// curr += prev --pos change
int i = 0, sum = 0, takeover = 0;
for (i = 0; i < pos; i++) {
sum = prev[i] + curr[i] + takeover;
curr[i] = sum % 10;
takeover = sum / 10;
}
if (takeover > 0) {
if (takeover > 10)
System.err.println("takeover too big");
curr[pos++] = takeover;
}
term++;
}
private static void print() {
int i = 0;
System.out.print("term " + term + " with pos " + pos + ": ");
for (i = pos - 1; i >= 0; i--) {
System.out.print(curr[i]);
}
System.out.println();
}
private static void sln2() {
boolean found = false;
BigInteger last = BigInteger.ONE;
BigInteger secLast = BigInteger.ONE;
BigInteger testNum;
long a = 3;
while (!found) {
testNum = last.add(secLast);
secLast = last;
last = testNum;
long digs = numDigits(testNum);
if (digs == 1000) {
// System.out.println(testNum + " has 1000 digits.");
System.out.println("This is f_" + a);
found = true;
} else {
// System.out.println(testNum);
a++;
}
}
}
private static long numDigits(BigInteger testNum) {
long digits = testNum.toString().length();
return digits;
}
/**
* @param args
*/
public static void main(String[] args) {
init();
long start = System.nanoTime();
while (pos < 1000) {
getNextFeb();
// print();
}
long end = System.nanoTime();
System.out.println("my time elapse: " + (end - start) + " nano secs");
print();
start = System.nanoTime();
sln2();
end = System.nanoTime();
System.out.println("Big Integer time elapse: " + (end - start)
+ " nano secs");
}
}