일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Geogebra
- project euler
- java
- 몬테카를로
- 재귀함수
- 프로젝트 오일러
- 프랙탈
- 피타고라스 정리
- python
- 작도
- 하합
- 큰 수의 법칙
- 정오각형
- 이항분포
- 파이썬
- 큰수의법칙
- 삼각함수의그래프
- 구분구적법
- 리만합
- 수학탐구
- algeomath
- 알지오매스
- 오일러
- counting sunday
- 제곱근의뜻
- 시뮬레이션
- 블록코딩
- 지오지브라
- 확률실험
- 상합
Archives
- Today
- Total
이경수 선생님의 수학실험실
Problem 20 (Factorial digit sum) 본문
Problem 20 (Factorial digit sum)
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!
In Python:
# Power digit sum
import time
startTime = time.time()
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
strNum = str(factorial(100))
numList = [int(num) for num in strNum]
print(sum(numList))
print(time.time() - startTime, "seconds")
Run time: 0.0001990795135498047 seconds
In Java:
//Euler20 Factorial digit sum
package project_euler11_20;
import java.math.BigInteger;
public class Euler20 {
public static BigInteger factorial(BigInteger n) {
if (n.compareTo(BigInteger.valueOf(1)) == 0) {
return BigInteger.valueOf(1);
} else {
return n.multiply(factorial(n.subtract(BigInteger.valueOf(1))));
}
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int sum = 0;
BigInteger num = factorial(BigInteger.valueOf(100));
String numString = num.toString();
for (int i = 0; i < numString.length(); i++) {
sum += Integer.parseInt(numString.charAt(i) + "");
}
System.out.println(sum);
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime) / (double)1000 + "seconds");
}
}
Run time: 0.004seconds
Solution: 648
'Project Euler' 카테고리의 다른 글
Problem 22(Names scores) (0) | 2019.04.14 |
---|---|
Problem 21(Amicable numbers) (0) | 2019.04.14 |
problem19 (Counting Sundays) (0) | 2019.02.16 |
problem18 (Maximum path sum I) (0) | 2019.02.16 |
problem17 (Number letter counts) (0) | 2019.02.14 |
Comments