forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p065.java
47 lines (37 loc) · 963 Bytes
/
p065.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
/*
* Solution to Project Euler problem 65
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.math.BigInteger;
public final class p065 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p065().run());
}
public String run() {
BigInteger n = BigInteger.ONE;
BigInteger d = BigInteger.ZERO;
for (int i = 99; i >= 0; i--) {
BigInteger temp = BigInteger.valueOf(continuedFractionTerm(i)).multiply(n).add(d);
d = n;
n = temp;
}
int sum = 0;
while (!n.equals(BigInteger.ZERO)) {
BigInteger[] divrem = n.divideAndRemainder(BigInteger.TEN);
sum += divrem[1].intValue();
n = divrem[0];
}
return Integer.toString(sum);
}
private static int continuedFractionTerm(int i) {
if (i == 0)
return 2;
else if (i % 3 == 2)
return i / 3 * 2 + 2;
else
return 1;
}
}