// n! means n X (n - 1) X ... X 3 X 2 X 1.
// For example, 10! = 10 X 9 X ... X 3 X 2 X 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!.
import java.math.BigInteger;
public class ProjectEuler_020 {
public static void main(String[] args) {
// 1부터 100까지의 숫자를 모두 곱한 결과를 저장하는 BigInteger 변수를 생성합니다.
BigInteger result = new BigInteger("1");
// 1부터 100까지의 숫자를 차례로 result에 곱합니다.
for (int i = 1; i <= 100; i++) {
// 현재 숫자를 문자열로 변환하여 BigInteger 객체로 생성하고 result에 곱합니다.
String variable = String.valueOf(i);
result = result.multiply(new BigInteger(variable));
}
// 결과를 출력합니다.
System.out.println(result);
// 결과를 문자열로 변환합니다.
String r = "" + result;
// 문자열로 변환된 결과를 각 자릿수별로 분리하여 정수 배열에 저장합니다.
int[] numArr = new int[r.length()];
for (int index = 0; index < r.length(); index++) {
numArr[index] = r.charAt(index) - '0';
}
// 각 자릿수의 합을 계산합니다.
int sum = 0;
for (int arrInd = 0; arrInd < numArr.length; arrInd++) {
sum += numArr[arrInd];
}
// 합계를 출력합니다.
System.out.println(sum);
}
}
Big integer에 대해 연산을 할 수 있는지 확인하는 문제.
'잡다한 코딩 > Project Euler (Java)' 카테고리의 다른 글
| (Java) Project Euler 022 - Names Scores (0) | 2023.12.31 |
|---|---|
| (Java) Project Euler 021 - Amicable Numbers (0) | 2023.12.30 |
| (Java) Project Euler 019 - Counting Sundays (0) | 2023.12.28 |
| (Java) Project Euler 018 - Maximum Path Sum I (1) | 2023.12.27 |
| (Java) Project Euler 017 - Number Letter Counts (1) | 2023.12.26 |