-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem020.java
28 lines (24 loc) · 883 Bytes
/
Problem020.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
import java.math.BigInteger;
import java.util.stream.Stream;
/**
* Problem 20:
* 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!
*/
public class Problem020 {
private static int sumDigitsFactorial(int n) {
return Stream.iterate(BigInteger.ONE, i -> i.add(BigInteger.ONE))
.limit(n)
.reduce(BigInteger.ONE, BigInteger::multiply)
.toString()
.chars()
.map(Character::getNumericValue)
.sum();
}
public static void main(String[] args) {
System.out.println("The sum of the digits in the number 100! is:");
System.out.println(sumDigitsFactorial(100));
}
}