forked from wandering007/ProjectEuler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP65.java
More file actions
45 lines (44 loc) · 1.21 KB
/
P65.java
File metadata and controls
45 lines (44 loc) · 1.21 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
import java.math.BigInteger;
public class P65
{
public static void main( String[] args )
{
long start = System.currentTimeMillis();
BigInteger numerator = new BigInteger("1");//初始化时为第100个分数的值
BigInteger denominator = new BigInteger("1");
BigInteger temp;
int num = 98, k = 0;//注意:要除去第零个,故不是99,而是98
while(num > 0)
{
switch(num % 3)
{
case 1:
case 0:
k = 1; break;
case 2:
k = (num + 2) / 3 * 2;
//break;
}
temp = denominator;
denominator = denominator.multiply(new BigInteger(k + "")).add(numerator);
numerator = temp;
num--;
}
numerator = denominator.multiply(new BigInteger("2")).add(numerator);
int res = SumOfDigits(numerator);
System.out.println(res);
//System.out.println(numerator.doubleValue() / denominator.doubleValue());
System.out.println("time cost: " + (System.currentTimeMillis() - start) + "ms");
}
private static int SumOfDigits(BigInteger n) {
int res = 0;
BigInteger zero = new BigInteger("0");
BigInteger ten = new BigInteger("10");
while(n.compareTo(zero) != 0)
{
res += n.mod(ten).intValue();
n = n.divide(ten);
}
return res;
}
}