-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler020.java
More file actions
executable file
·36 lines (31 loc) · 1014 Bytes
/
Copy pathEuler020.java
File metadata and controls
executable file
·36 lines (31 loc) · 1014 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
import java.math.BigInteger;
import java.util.Date;
/*
Project Euler Problem 20
========================
n! means n * (n - 1) * ... * 3 * 2 * 1
Find the sum of the digits in the number 100!
*/
public class Euler020 {
public static void main(String[] args) {
Date start, end;
start = new Date();
BigInteger integer = factorial(100);
BigInteger sum = new BigInteger("0");
while (!integer.equals(new BigInteger("0"))) {
sum = sum.add(integer.mod(new BigInteger("10")));
integer = integer.divide((new BigInteger("10")));
}
end = new Date();
System.out.println(sum);
System.out.println("Execution Time: "
+ (end.getTime() - start.getTime()));
}
public static BigInteger factorial(int n) {
BigInteger fact = new BigInteger("1");
for (int i = 1; i <= n; i++) {
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}
}