forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp092.py
More file actions
40 lines (29 loc) · 721 Bytes
/
p092.py
File metadata and controls
40 lines (29 loc) · 721 Bytes
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
#
# Solution to Project Euler problem 92
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import sys
if sys.version_info.major == 2:
range = xrange
def compute():
ans = sum(1
for i in range(1, 10000000)
if get_terminal(i) == 89)
return str(ans)
TERMINALS = (1, 89)
def get_terminal(n):
while n not in TERMINALS:
n = square_digit_sum(n)
return n
def square_digit_sum(n):
result = 0
while n > 0:
result += SQUARE_DIGITS_SUM[n % 1000]
n //= 1000
return result
SQUARE_DIGITS_SUM = [sum(int(c)**2 for c in str(i)) for i in range(1000)]
if __name__ == "__main__":
print(compute())