-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler92.py
More file actions
50 lines (35 loc) · 1.14 KB
/
Copy patheuler92.py
File metadata and controls
50 lines (35 loc) · 1.14 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
'''
Square digit chains
Problem 92
A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.
For example,
44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.
How many starting numbers below ten million will arrive at 89?
'''
import math
found = 0
leads_to_89 = set([89])
leads_to_1 = set([1])
def add_sq_of_digit(number):
return sum([int(n) ** 2 for n in list(str(number))])
def arrives_at_89(number):
if number in leads_to_89:
return True
if number in leads_to_1:
return False
path = [number]
while True:
path.append(add_sq_of_digit(path[-1]))
if path[-1] in leads_to_89:
leads_to_89.update(path)
return True
if path[-1] in leads_to_1:
leads_to_1.update(path)
return False
for number in range (10 ** 7, 2, -1):
#print("testing:", number)
#print(len(leads_to_1), len(leads_to_89))
arrives_at_89(number)
print(len(leads_to_89))