-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 62
More file actions
61 lines (52 loc) · 1.29 KB
/
Copy pathproblem 62
File metadata and controls
61 lines (52 loc) · 1.29 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
51
52
53
54
55
56
57
58
59
60
"""
The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
"""
"""
import math
from itertools import permutations
solution = [0] * 10000
def cube(num, all_cube):
num = str(num)
num = sorted(num)
for i in all_cube:
if num == i:
solution[int(round(int(''.join(num)) ** (1. / 3)))] += 1
return solution
all_cube = []
for i in range(1, 10000):
num = i ** 3
num = str(num)
num = sorted(num)
all_cube.append(num)
for i in range(1, 10000):
cube(i ** 3, all_cube)
for i in range(1000):
if solution[i] == 5:
print(i ** 3) """
from collections import defaultdict
import time
start = time.time()
cubes = defaultdict(set)
i = 0
while True:
key = "".join(sorted(list(str(i**3))))
cubes[key].add(i**3)
if len(cubes[key]) == 5:
print(key, min(cubes[key]))
break
i += 1
end = time.time()
print(end - start)
start = time.time()
cubes = []
i = 0
while True:
cube = sorted(list(str(i ** 3)))
cubes.append(cube)
if cubes.count(cube) == 5:
print(cubes.index(cube) ** 3)
break
i += 1
end = time.time()
print(end - start)