-
Notifications
You must be signed in to change notification settings - Fork 677
/
p062.py
33 lines (26 loc) · 845 Bytes
/
p062.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
#
# Solution to Project Euler problem 62
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import itertools
def compute():
numdigits = 0
data = {} # str numclass -> (int lowest, int count)
for i in itertools.count():
digits = [int(c) for c in str(i**3)]
digits.sort()
numclass = "".join(str(d) for d in digits)
if len(numclass) > numdigits:
# Process and flush data for smaller number of digits
candidates = [lowest for (lowest, count) in data.values() if count == 5]
if len(candidates) > 0:
return str(min(candidates)**3)
data = {}
numdigits = len(numclass)
lowest, count = data.get(numclass, (i, 0))
data[numclass] = (lowest, count + 1)
if __name__ == "__main__":
print(compute())