-
Notifications
You must be signed in to change notification settings - Fork 1
/
p62.erde
45 lines (39 loc) · 956 Bytes
/
p62.erde
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
-- Sort digits of n into a string
local function sortDigits(n) {
local digits = {}
while n > 0 {
table.insert(digits, math.floor(n % 10))
n = math.floor(n / 10)
}
table.sort(digits)
local result = ""
for i = 1, #digits {
result ..= digits[i]
}
return result
}
-- Use sorted digits as key to keep track of number of cubes (and smallest cube)
local function solve() {
local desiredCount = 5
local n = 1
local data = {}
while true {
-- Note: Need to ensure float here!
local cube = 1.0 * n * n * n
local digits = sortDigits(cube)
local d = data[digits]
if d {
d.count += 1
if d.count >= desiredCount {
return d.smallest
}
} else {
d = {}
d.smallest = cube
d.count = 1
data[digits] = d
}
n += 1
}
}
print(solve())