[Python] 20 – Factorial digit sum

[문제 링크]
http://projecteuler.net/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!

[풀이방법]
1. 반복문을 통해 팩토리얼의 값을 구한다.
2. 팩토리얼 값을 String 타입으로 변경한다.
3. 변경된 String 값을 한자씩 추출하여 Integer 타입으로 변경한다.
4. 변경된 Integer 타입을 더한다.

[소스코드]

#20_FactorialDigitSum.py

num = 100
sum = 1
while(num > 0) :
	sum = sum * num
	num = num -1

strNum = str(sum)
num = 0
for i in range(0, len(strNum)):
	#print(strNum[i], end=' ')
	num = num  + int(strNum[i])

print("DigitSum=", num)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s