# https://projecteuler.net/problem=62
from projecteuler.NumberProperties import is_permutation

# the cubes of 1-9999, sorted in
# sub-arrays by number of digits
cubes = [[1]]

for n in [i**3 for i in range(2, 10000)]:
    if len(str(n)) != len(str(cubes[-1][0])):
        cubes.append([])
    cubes[-1].append(n)

def answer():
    for length in cubes:
        for idx in range(len(length)):
            count = 1
            n1 = length[idx]
            for n2 in length[idx + 1:]:
                if is_permutation(n1, n2):
                    count += 1
            if count == 5:
                return n1

print(answer())
