Programming/Project Euler - python
Project Euler 20
_Sudal
2019. 3. 8. 05:00
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!
문제:
n! 이라는 표기법은 n × (n − 1) × ... × 3 × 2 × 1을 뜻합니다.
예를 들자면 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800 이 되는데,
여기서 10!의 각 자리수를 더해 보면 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27 입니다.100! 의 자리수를 모두 더하면 얼마입니까?
Solution:
1 2 3 4 5 | from math import factorial import time start = time.time() print(eval("+".join(list(str(factorial(100)))))) print('Found in ... {:.5f}s'.format(time.time() - start)) | cs |
100! 을 string 으로 만든 후, list 처리를 하고 join을 이용하여 사이사이에 +를 넣어주고 eval 함수를 써주었다.
eval 함수는 여기서도 썼었다.
2019/03/03 - [Programming/Project Euler - python] - Project Euler 16
0.0003s 걸렸다!