파이썬 실전 프로젝트

닫기

# 프로젝트 오일러 20번 문제 - 팩토리얼 자릿수 합

100 팩토리얼을 계산한후, 각 자리수를 더하는 문제입니다.

## 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!

##### 팩토리얼 계산
```def fact(num):
total=1
while num>0:
total*=num
num-=1

fact(10)```
`3628800`

```def fact(num):
total=1
for i in range(1,num+1):
total*=i

fact(10)```
`3628800`

```def fact(num):
if num==1:
return 1
return num*fact(num-1)

fact(10)```
`3628800`

```from math import factorial
factorial(10)```
`3628800`

##### 자릿수 합
```def fact(num):
total=1
for i in range(1,num+1):
total*=i

result=fact(10)
string=str(result)

total=0
for i in string:
total+=int(i)
print(total)```
`27`

##### 100 팩토리얼 계산
```def fact(num):
total=1
for i in range(1,num+1):
total*=i

result=fact(100)
print(result)
string=str(result)

total=0
for i in string:
total+=int(i)
print(total)```
```93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
648```

댓글 본문
버전 관리