-
Notifications
You must be signed in to change notification settings - Fork 0
/
p092.py
40 lines (34 loc) · 966 Bytes
/
p092.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
# Would like to speed this up by not using brute force approach
from math import log10
def sum_digit_squares(n):
""" Returns sum of square of each digit in number n.
>>> sum_digit_squares(11)
2
>>> sum_digit_squares(93)
90
"""
total = 0
while n > 0:
total += (n % 10)**2
n //= 10
return total
def compute():
LIMIT = 10_000_000
# max sum for 7 digit number is 9_999_999 case which is 81 * 7 where 7 = log10(10**7)
arrivals = [0] * (9**2 * int(log10(LIMIT)) + 1)
arrivals[1] = 1
total = 0
for i in range(1, LIMIT + 1):
start = i
while i != 1 and i != 89:
if i < len(arrivals) and arrivals[i]:
i = arrivals[i]
break
i = sum_digit_squares(i)
if start < len(arrivals):
arrivals[start] = i
if i == 89:
total += 1
return total
if __name__ == "__main__":
print(compute())