-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmult_per.py
More file actions
100 lines (89 loc) · 2.5 KB
/
mult_per.py
File metadata and controls
100 lines (89 loc) · 2.5 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Tool for calculating the multiplicative persistance of a given integer
# Tyler Olson
# March 22, 2019
# Revision 1.1 ~ change n_list handling for better performance
# Inspired by Numberphile: https://www.youtube.com/watch?v=Wim9WJeDTHQ
import time
def per(n_list, steps=0):
#print(n)
if len(n_list) == 1:
#print("DONE. Steps: {0}".format(steps))
return steps
else:
res = 1
for d in n_list:
res *= d
steps += 1
return per(listify(res), steps)
def seed(num_digits):
'''Smallest non-trivial value for a given number of digits above 3.
Returns a list of digits'''
return [2] + [6] * (num_digits - 1)
def listify(n):
'''Takes in an integer and returns a list of all of it's digits'''
return [int(digit) for digit in str(n)]
def iterate(n_list):
if n_list[1] == 9:
poss = [2,3,4,6,7,8,9]
first = n_list[0] + 1
if first > 9:
return None
if first == 5:
first = 6
n_list = [6 for i in range(len(n_list))]
n_list[0] = first
else:
n_list = _iterate(n_list)
#print(ret_val)
return n_list
def _iterate(n_list):
last = n_list.pop()
if last < 9:
n_list.append(last + 1)
else:
n_list = _iterate(n_list)
last = n_list.pop()
n_list.append(last)
n_list.append(last)
return n_list
def processor(n_list):
found = False
while not found:
steps = per(n_list)
if steps < 11:
n_list = iterate(n_list)
if n_list is None:
print('None found')
found = True
else:
n = 0
for d in n_list:
n = n * 10 + d
#print(ret_val)
print('Steps:', steps, n)
found = True
return n
def main():
'''Processes 10^'reps' values, starting at num_digits.'''
timers = []
num_digits = 245
reps = 50
# for i in range(20):
# main(2 + i)
start_1 = time.time()
for i in range(reps):
start = time.time()
n_list = seed(num_digits + i)
n = processor(n_list)
if n is not None:
success = n
end = time.time()
print('SUCCESS!!!:', success, 'TIME:', end - start)
break
end = time.time()
timers.append(end - start)
print(num_digits + i, end - start)
print(timers)
print('Total Elapsed time: ', end - start_1)
if __name__ == '__main__':
main()