forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp025.java
More file actions
41 lines (32 loc) · 950 Bytes
/
p025.java
File metadata and controls
41 lines (32 loc) · 950 Bytes
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
/*
* Solution to Project Euler problem 25
* by Project Nayuki
*
* http://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.math.BigInteger;
public final class p025 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p025().run());
}
private static final int DIGITS = 1000;
public String run() {
BigInteger lowerthres = BigInteger.TEN.pow(DIGITS - 1);
BigInteger upperthres = BigInteger.TEN.pow(DIGITS);
BigInteger prev = BigInteger.ONE;
BigInteger cur = BigInteger.ZERO;
int i = 0;
while (true) {
// At this point, prev = fibonacci(i - 1) and cur = fibonacci(i)
if (cur.compareTo(lowerthres) >= 0)
return Integer.toString(i);
else if (cur.compareTo(upperthres) >= 0)
throw new RuntimeException("Not found");
BigInteger temp = cur.add(prev);
prev = cur;
cur = temp;
i++;
}
}
}