-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp092.py
More file actions
executable file
·45 lines (36 loc) · 1.03 KB
/
p092.py
File metadata and controls
executable file
·45 lines (36 loc) · 1.03 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
#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 logging
logger = logging.getLogger('p092')
sn1 = set([44, 32, 13, 10, 1])
sn89 = set([85, 89, 145, 42, 2, 20, 4, 16, 37, 58])
def SquareNum(n):
s = 0
while (n):
d = n%10
s += d*d
n = n // 10
if (s in sn1):
return 1
if (s in sn89):
return 89
if (SquareNum(s) == 1):
sn1.add(s)
return 1
sn89.add(s)
return 89
def main(args):
m = 1000*1000*10
s = 0
for n in range(2, m):
if (SquareNum(n) == 89):
s += 1
logger.info("answer:{}".format(s))