import itertools
import math

# https://stackoverflow.com/questions/396421/checking-if-two-strings-are-permutations-of-each-other-in-python
import collections
def same_permutation(a, b):
    #print (a, b)
    d = collections.defaultdict(int)
    for x in a:
        d[x] += 1
    for x in b:
        d[x] -= 1
    #return not any(d.itervalues())
    return not any(d.values())

#print(same_permutation([1,2,3],[2,3,1]))
#print(same_permutation([1,2,3],[2,3,1,1]))
#exit(0)

def int_to_list_of_ints(i):
    return list(map(int, list(str(i))))

def f(_len):
    array=set()

    for i in range(1, 10**7):
        if len(str(i**3))==_len:
            array.add(i**3)
        if len(str(i**3))==_len+1:
            break

    print (f"{len(array)=}")

    stat=collections.defaultdict(list)
    for x in itertools.combinations(array, 2):
        pair1=x[0]
        pair2=x[1]
        lst1=int_to_list_of_ints(pair1)
        lst2=int_to_list_of_ints(pair2)
        if same_permutation(lst1, lst2):
            stat[x[0]].append(x[1])

    for s in stat:
        #if len(stat[s])==3-1:
        if len(stat[s])==5-1:
            print (s, stat[s])

for l in range(5, 50):
    print (f"{l=}")
    f(l)

