-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path065.java
More file actions
74 lines (61 loc) · 1.83 KB
/
065.java
File metadata and controls
74 lines (61 loc) · 1.83 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
package euler;
import java.math.BigInteger;
public class euler {
public static class Fraction {
BigInteger numerator;
BigInteger denominator;
public Fraction () {}
public Fraction (BigInteger n, BigInteger d) {
this.numerator=n;
this.denominator=d;
}
public Fraction add (Fraction f) {
BigInteger lcm=this.denominator.multiply(f.denominator).divide(this.denominator.gcd(f.denominator));
Fraction newF=new Fraction();
newF.numerator=lcm.divide(this.denominator).multiply(this.numerator).add(lcm.divide(f.denominator).multiply(f.numerator));
newF.denominator=lcm;
return newF;
}
public Fraction invert () {
Fraction newF=new Fraction();
newF.numerator=this.denominator;
newF.denominator=this.numerator;
return newF;
}
}
//e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
public static int getA (int depth) {
if (depth==0) {
return 2;
} else if (depth%3==2) {
return 2*((depth/3)+1);
} else {
return 1;
}
}
public static int max=0;
public static Fraction expandHelper (int depth) {
if (depth==max) {
return new Fraction(BigInteger.ONE,BigInteger.valueOf(getA(depth)));
} else {
Fraction ex=expandHelper(depth+1);
return new Fraction(BigInteger.valueOf(getA(depth)),BigInteger.ONE).add(ex).invert();
}
}
public static Fraction expand (int maxDepth) {
max=maxDepth-1;
return expandHelper(0).invert();
}
public static void main (String [] zzzz) {
long before=System.currentTimeMillis();
Fraction result=expand(100);
BigInteger numerator=result.numerator;
BigInteger sum=BigInteger.ZERO;
while (!numerator.equals(BigInteger.ZERO)) {
sum=sum.add(numerator.mod(BigInteger.TEN));
numerator=numerator.divide(BigInteger.TEN);
}
System.out.println(sum.toString());
System.out.println("Took "+(System.currentTimeMillis()-before)+"ms.");
}
}