-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem20.java
More file actions
40 lines (33 loc) · 1.06 KB
/
Problem20.java
File metadata and controls
40 lines (33 loc) · 1.06 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
import java.math.BigInteger;
/**
*
* @author rifansyah
*/
public class Problem20 {
public static void main(String[] args) {
String factorial = getFactorial(100);
int sumOfFactorial = getSumOfFactorialDigits(factorial);
System.out.print("Sum of factorial digits : " + sumOfFactorial);
}
public static String getFactorial(int num) {
BigInteger result = BigInteger.valueOf(1);
for(int i = 1; i <= num; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
return result.toString();
}
public static int getSumOfFactorialDigits(String num) {
int result = 0;
for(int i = 0; i < num.length(); i++) {
result += Integer.parseInt("" + num.charAt(i));
}
return result;
}
}
// question :
/*
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
*/