-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_problem_20.py
58 lines (46 loc) · 1.2 KB
/
euler_problem_20.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'''
Factorial digit sum
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!
'''
digits = [0 for i in range(300)]
digits[0] = 1
def copyList(oldList, newList):
for i in range(len(newList)):
oldList[i] = newList[i]
def multiply(num, digits):
i = 0
tempDigits = [0 for j in range(300)]
for digit in digits:
result = num*digit
if not result == 0:
addToArrayNum(result, i, tempDigits)
i += 1
copyList(digits, tempDigits)
def addToArrayNum(num, tenToThePowerOf, tempDigits):
i = tenToThePowerOf
carryOverVal = 0
while (num > 0):
tempVal = tempDigits[i] + (num % 10) + carryOverVal
if tempVal < 10:
tempDigits[i] = tempVal
carryOverVal = 0
else:
tempDigits[i] = (tempVal % 10)
carryOverVal = (int)(tempVal/10)
num = (int)(num/10)
i += 1
if carryOverVal > 0:
tempVal = tempDigits[i] + carryOverVal
if tempVal < 10:
tempDigits[i] = tempVal
else:
tempDigits[i] = (tempVal % 10)
def factorial(num):
for i in range(1, num+1):
multiply(i, digits)
factorial(100)
print reduce(lambda x, y: x+y, digits)