-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsolution_1.py
More file actions
87 lines (63 loc) · 2.05 KB
/
solution_1.py
File metadata and controls
87 lines (63 loc) · 2.05 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# coding=utf-8
# Python Script
#
# Copyleft © Manoel Vilela
#
#
"""
Cubic permutations
Problem 62
The cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3). 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.
"""
def cubesGen(n, start=1):
return map(lambda x: str(x*x*x), range(start, n + 1))
def getId(n):
return sum([2 ** int(x) for x in n]) * sum([int(x) for x in list(n)])
def isPerm(a, b):
for i in a:
for j in b:
if i not in b or j not in a:
return False
return getId(a) == getId(b)
def cubeRoot(n):
return int(n ** (1/3) + 0.00001)
def mostCube(n):
most = int(''.join(sorted(n, reverse=True)))
return cubeRoot(most)
def lowerCube(n):
lower = int(''.join(sorted(n)))
return cubeRoot(lower)
# my_solution... after much hard work the results final is this...
# i want to break free XD
def solution(max):
maxCube = 10 ** 15
for cube_a in cubesGen(maxCube):
permCubes = 0
for cube_b in cubesGen(int(mostCube(cube_a)), start=int(lowerCube(cube_a))):
if isPerm(cube_a, cube_b):
permCubes += 1
if permCubes == max:
return cube_a
#the solution of one guy, much more efficient.
def solution_extern():
def cubes():
i = 1
while True:
yield i * i * i
i += 1
cube_dict = {}
for c in cubes():
digits = ''.join(sorted(str(c)))
if digits in cube_dict:
cube_list = cube_dict[digits]
cube_list.append(c)
if(len(cube_list)) == 5:
return min(cube_list)
break
else:
cube_dict[digits] = [c]
print(solution_extern())
#wrong answer: 1000600120008 (because its included the self number, this answer contain 6 permutations)
#correct answer: 127035954683 (5 permutations!)