SageMath 如何修复正确生成出租车号码的列表?

SageMath How to fix List for Taxicab numbers to be properly generated?

我正在尝试在 SageMath 中编写一个函数来打印所有 Taxicab 数字(等于多组两个值的立方之和然后相加的数字)小于或等于某个值(在我的代码中我将此数字称为变量 t).

我似乎无法弄清楚如何在 SageMath 中对列表(我最初在 Python 2.7 中编写的列表)进行适当的更改以 运行。结果我不断收到错误:

Traceback (most recent call last):            for i in range(1,len(sums)-1):
  File "", line 1, in <module>

File "/private/var/folders/96/g5hyl4ps29dglpy8fnwww6x80000gn/T/tmpWiTKG1/___code___.py", line 16, in <module>
exec compile(u'Ramanujan(_sage_const_10000 )
File "", line 1, in <module>

File "/private/var/folders/96/g5hyl4ps29dglpy8fnwww6x80000gn/T/tmpWiTKG1/___code___.py", line 7, in     Ramanujan
crev[x3] = x + _sage_const_1 ;
IndexError: list assignment index out of range

代码:

def Ramanujan(t):
    cubes = [x**3 for x in range(1,t/10)];
    crev = [] # Calculating Cube Roots;
    for x,x3 in enumerate(cubes):
        crev[x3] = x + 1;
        sums = sorted(x + y for x in cubes for y in cubes if y < x) # Organizing Data
        for i in range(1,len(sums)-1):
            if sums[i-1] != sums[i] and sums[i] == sums[i+1]: # Finding solutions
                if sums[i]<=t: # Limiting how many solutions printed.
                    print "%10d"%(sums[i]) # Printing desired outputs
                else:
                    break # Ending the function.

Ramanujan(10000)

(Ramanujan(10000) 应该使函数打印 2 个小于 10,000 的值)

我是否需要将函数中的变量声明为变量对象? 我不需要在填充之前创建空白列表 crev 吗? 这只是我尝试使用列表的方式的问题吗?

我认为行 crev[x3] = x + 1; 应该是 crev.append(x+1):如果列表的长度小于 n,则不能分配列表中的第 n 个元素。或者您应该使用 crev = [0]*t 创建它,以便它最初包含全零。但实际上,如果函数保持原样,则应完全删除该行:您永远不要在函数的其余部分使用 crev