Python 逻辑 - 欧拉 62

Python logic - Euler 62

我正在尝试通过解决 projecteuler.net 的问题来学习 python。我一直在尝试解决第 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.

我解决这个问题的逻辑:
- 生成最多 10000 个立方体并将它们存储在一个集合中
- 对于集合中的每个立方体(从最大的开始),找到可以使用数字中的数字组成的最大数字。如果两个立方体的数字相同,则它们将组成相同的最大数。
- 将这些最大数字存储在另一组中。如果最大数量已经在该集合中,则增加与该最大数量相对应的计数器。
- 如果计数器是 5,停止。

这是我为实现上述逻辑而编写的代码。它给了我 140283769536 的答案,但这不是正确的答案。我哪里错了?

def maxperm(number):
    maxnum=0
    digits=[0 for x in range(10)]
    while number>0:
        digits[number%10]+=1
        number/=10
    numdigits=sum(digits)
    for i in range(9,-1,-1):
        if digits[i]>0:
            for x in range(digits[i]):
                numdigits-=1
                maxnum+=i*10**numdigits
    return maxnum

uniquecubes=dict()
cubes=[x**3 for x in range(10000)]
cubeset=set(cubes)
maxperms=set()

for i in reversed(cubes):
    a=maxperm(i)
    if a in maxperms:
        uniquecubes[a]+=1
        if uniquecubes[a]==5:
            print i
            break
    else:
        uniquecubes[a]=1
        maxperms.add(a)

该算法旨在查找是否存在 5 个 最小 排列也是立方体,这不是要问的问题。

解决方案 1:修改算法以检查是否存在第 6 个,如果存在,则丢弃答案并继续寻找。

解决方案 2(首选):有一个函数 permCheck() 接受一个立方体,创建该立方体数字的每个排列,以及 returns 这些排列中有多少也是立方体。一旦你拥有了:

int i = 1
while True:
    if permCheck(i*i*i) == 5:
        break
    i += 1
return i*i*i  # i^3 is now the answer