我对编程比较陌生,我正在尝试使用这个公式生成一个数字列表。
如果“i”是列表的索引,则公式为 list[i] = list[i-2] + list[i-3]。如果您从 1,1,1 开始,前几个数字将如下所示。
1,1,1,2,2,3,4,5,7,9,12,16,21,28,37,49,65,86.等 要获取每个数字(在 1、1、1 之后),您可以跳过一个数字,然后取前两个数字的总和,例如 49 来自 21 和 28 的总和。
寻找数字的过程类似于斐波那契,但这些数字是完全不同的。
我的代码如下所示:
start = [1,1,1] #the list must start with three 1's
list1 = start #list1 starts with 'start'
newList = []
ammountOfNumbers = int(raw_input("Enter the ammount of numbers to be generated(n >= 3): "))# to dictate length of generated list
def generateList(newList, aList, ammountOfNumbers, *a):
while len(aList) <= ammountOfNumbers: #while length of list is less than or = size of list you want generated
for x in range((ammountOfNumbers-1)):
newList.append(x) #this puts value of x in index '0'
newList[x] = aList[len(aList)-1] + aList[len(aList)-2] # generate next number
aList += newList #add the next generated number to the list
x+=1
print
#print "Inside: ", aList #test
#print "Length inside: ",len(aList) #test
print
return aList
final = generateList(newList, list1, ammountOfNumbers) # equal to the value of list1
print"Final List: " , final
print
print"Length Outside: ", len(final) #wrong value
它现在显然不能正常工作。我希望能够生成大约 500 个这些数字的列表。有没有人有什么建议?谢谢!