-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem34.py
84 lines (59 loc) · 1.81 KB
/
Problem34.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Code by @AmirMotefaker
# projecteuler.net
# https://projecteuler.net/problem=34
# Digit factorials
# Problem 34
# 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
# Find the sum of all numbers which are equal to the sum of the factorial of their digits.
# Note: As 1! = 1 and 2! = 2 are not sums they are not included.
# Solution 1
# from math import factorial #factorial function for calculating factorial
# import time
# start_time = time.time() #Time at the start of program execution
# #factorials of numbers from 0-9
# f = [factorial(0),
# factorial(1),
# factorial(2),
# factorial(3),
# factorial(4),
# factorial(5),
# factorial(6),
# factorial(7),
# factorial(8),
# factorial(9)]
# def factorial_digits(n): #sum of factorial of the digits
# s = 0
# while n:
# s += f[n%10]
# n //= 10
# return s
# solution = 0 #variable to save the value of solution
# for i in range(10,1854721): #for loop to loop till 1854721
# if factorial_digits(i) == i:
# solution += i
# print (solution)
# end_time = time.time() #Time at the end of execution
# print ("Time of program execution:", (end_time - start_time)) # Time of program execution
# Solution 2
import time
start_time = time.time() #Time at the start of program execution
def factorial(n):
if n == 1 or n == 0:
return 1
else:
return n*factorial(n-1)
def main():
final_total = 0
for i in range(10, 100000):
str_i = str(i)
total = 0
for digit in str_i:
digit = int(digit)
total += factorial(digit)
if total == i:
final_total += i
print(i, final_total)
main()
end_time = time.time() #Time at the end of execution
print ("Time of program execution:", (end_time - start_time)) # Time of program execution
### Answer: 40730