Python 斐波那契加法器-计算斐波那契序列中的数字
我正在开发一个程序,该程序将计算具有某些数字限制的斐波那契数(即第一个100位数的斐波那契数)。下面的代码正在运行,但我遇到了一个逻辑错误,这让我很困惑 代码的目标是以类似于二进制加法的方式计算斐波那契数。使用数组时,每个元素都要包含0-9之间的数字,因此每个数组索引表示10的位置 它开始工作,并通过罚款循环,但它得到了13和21之间,因为循环的方式处理。它将10位的数字相加,然后保存一个31的数字 有没有办法打破或阻止它把我看不到的东西加在一起Python 斐波那契加法器-计算斐波那契序列中的数字,python,loops,fibonacci,Python,Loops,Fibonacci,我正在开发一个程序,该程序将计算具有某些数字限制的斐波那契数(即第一个100位数的斐波那契数)。下面的代码正在运行,但我遇到了一个逻辑错误,这让我很困惑 代码的目标是以类似于二进制加法的方式计算斐波那契数。使用数组时,每个元素都要包含0-9之间的数字,因此每个数组索引表示10的位置 它开始工作,并通过罚款循环,但它得到了13和21之间,因为循环的方式处理。它将10位的数字相加,然后保存一个31的数字 有没有办法打破或阻止它把我看不到的东西加在一起 num1 = [0]*100 num2 = [0
num1 = [0]*100
num2 = [0]*100
num2[len(num2)-1] = 1
carry = 0
flag = True
while (flag):
#Main for loop to work through the array
for count in range (1, len(num2)):
carry = num2[len(num2) - count] + num1[len(num1) - count]
if carry > 9:
num2[len(num2)- (count + 1)] = num2[len(num2)- (count + 1)] + 1
carry = carry % 10
num1[len(num1) - count] = num2[len(num2) - count]
num2[len(num2) - count] = carry
else:
num1[len(num1) - count] = num2[len(num2) - count]
num2[len(num2) - count] = carry
print(num2)
if num2[0] != 0:
flag = False
每次它通过主回路时,我都希望看到
[0,0,...,0,1]
[0,0,...,0,2]
[0,0,...,0,3]
[0,0,...,0,5]
[0,0,...,0,8]
[0,0,...,1,3]
[0,0,...,2,1]
...
但是在它到达[…,2,1]循环之后,它会转到[…,3,1]这里有一个更清晰的版本,我相信你正在尝试得到它
#Init of Fib variables
a = 0
b = 1
num = 10 #Change this to the number of fib calculation loops.
x = 0
output_arr_len = 100 #Change this to 10 for testing, as it's easier to see.
while x < num:
#Calculate Fib Sequence
c = a + b
a = b
b = c
x += 1
#Output Array
print([0] * (output_arr_len - len(str(c))) + [int(i) for i in str(c)])
这里有一个更清晰的版本,我相信你正试图得到的
#Init of Fib variables
a = 0
b = 1
num = 10 #Change this to the number of fib calculation loops.
x = 0
output_arr_len = 100 #Change this to 10 for testing, as it's easier to see.
while x < num:
#Calculate Fib Sequence
c = a + b
a = b
b = c
x += 1
#Output Array
print([0] * (output_arr_len - len(str(c))) + [int(i) for i in str(c)])
这甚至比我的评论更棘手,但这个版本工作正常:
num1 = [0]*10
num2 = [0]*10
num2[len(num2)-1] = 1
sum = 0
carry = 0
flag = True
while (flag):
#Main for loop to work through the array
for count in range (1, len(num2)):
sum = num2[len(num2) - count] + num1[len(num1) - count] + carry
num1[len(num1) - count] = num2[len(num2) - count]
if sum > 9:
sum = sum % 10
carry = 1
else:
carry = 0
num2[len(num2) - count] = sum
if carry == 1:
num2[0] = num2[0] + 1
print(num2)
if num2[0] != 0:
flag = False
在应用进位之前,您还必须复制到new1,即使是在下一个更高的位进行复制…这比我的评论中更棘手,但此版本工作正常:
num1 = [0]*10
num2 = [0]*10
num2[len(num2)-1] = 1
sum = 0
carry = 0
flag = True
while (flag):
#Main for loop to work through the array
for count in range (1, len(num2)):
sum = num2[len(num2) - count] + num1[len(num1) - count] + carry
num1[len(num1) - count] = num2[len(num2) - count]
if sum > 9:
sum = sum % 10
carry = 1
else:
carry = 0
num2[len(num2) - count] = sum
if carry == 1:
num2[0] = num2[0] + 1
print(num2)
if num2[0] != 0:
flag = False
在应用进位之前,您还必须复制到new1,即使是在下一个更高的位执行此操作…以下是对代码的更正。注意Python有无限精度的整数,所以我添加了一个斐波那契生成器来检查答案
num1 = [0]*100
num2 = [0]*100
num2[len(num2)-1] = 1
flag = True
# Fibonacci generator for verification of answer
def fib():
a,b = 0,1
while True:
a,b = b,a+b
yield a
# Instance of the generator
f = fib()
# convert a list of single-digit integers to a real integer for display
def value(L):
assert all(n < 10 for n in L) # bug checking for invalid list values.
return int(''.join([str(i) for i in L]))
while (flag):
#Main for loop to work through the array
# Start with zero carry for first digit
carry = 0
for count in range (1,len(num2)+1): # originally off-by-1.
# compute the sum plus the carry of previous sum
temp = num2[len(num2) - count] + num1[len(num1) - count] + carry
# shift num2 digit to num1 digit
num1[len(num1) - count] = num2[len(num2) - count]
# new num2 digit is the one's place of temp sum.
num2[len(num2) - count] = temp % 10
# remember the carry (10's place) for next sum.
carry = temp // 10
# Check for bugs...compare the answer with the next Fibonacci number
assert value(num1) == next(f)
if num1[0] != 0:
flag = False
print(value(num1))
这是对代码的更正。注意Python有无限精度的整数,所以我添加了一个斐波那契生成器来检查答案
num1 = [0]*100
num2 = [0]*100
num2[len(num2)-1] = 1
flag = True
# Fibonacci generator for verification of answer
def fib():
a,b = 0,1
while True:
a,b = b,a+b
yield a
# Instance of the generator
f = fib()
# convert a list of single-digit integers to a real integer for display
def value(L):
assert all(n < 10 for n in L) # bug checking for invalid list values.
return int(''.join([str(i) for i in L]))
while (flag):
#Main for loop to work through the array
# Start with zero carry for first digit
carry = 0
for count in range (1,len(num2)+1): # originally off-by-1.
# compute the sum plus the carry of previous sum
temp = num2[len(num2) - count] + num1[len(num1) - count] + carry
# shift num2 digit to num1 digit
num1[len(num1) - count] = num2[len(num2) - count]
# new num2 digit is the one's place of temp sum.
num2[len(num2) - count] = temp % 10
# remember the carry (10's place) for next sum.
carry = temp // 10
# Check for bugs...compare the answer with the next Fibonacci number
assert value(num1) == next(f)
if num1[0] != 0:
flag = False
print(value(num1))
它缺少num1的定义。它是num2吗(除了右边的0外都是0)?它就在那里,当我抓到要粘贴到这里的代码时,一定没有复制它。我将进行编辑,使其更清晰。@LukeMorris,我不太清楚您的输出应该是什么?在你的问题中,你能澄清一下吗?当num2=8,num1=5时,在第一次进位时,错误已经出现了:在count=1时,你将num2修改为13,然后在count=2时,你使用该1(进位)相加成num1,得到15,而不是预期的8。使用调试器;-)我想你可能记得有一个进位,然后在下一次计数时减去它,或者你需要改变你关于如何更新新的1和2的逻辑,我会说,记住在下一个循环中有一个进位要加,但不要在当前数字中加。这应该最有效!它缺少num1的定义。它是num2吗(除了右边的0外都是0)?它就在那里,当我抓到要粘贴到这里的代码时,一定没有复制它。我将进行编辑,使其更清晰。@LukeMorris,我不太清楚您的输出应该是什么?在你的问题中,你能澄清一下吗?当num2=8,num1=5时,在第一次进位时,错误已经出现了:在count=1时,你将num2修改为13,然后在count=2时,你使用该1(进位)相加成num1,得到15,而不是预期的8。使用调试器;-)我想你可能记得有一个进位,然后在下一次计数时减去它,或者你需要改变你关于如何更新新的1和2的逻辑,我会说,记住在下一个循环中有一个进位要加,但不要在当前数字中加。这应该最有效!您的答案是有效的,但对于任意大的值来说是无效的,因为您依赖于最大整数值。这个问题,实际上是一个数字一个数字地工作,而不是仅仅以那种方式显示,更为一般。…。@B.Go Python中没有最大整数值(取决于内存),这让我想知道OP为什么要这样做。@MarkTolonen好的,谢谢你的信息。但我仍然认为,使用与问题相同的算法会更好,并且应该修复该算法,而不是使用不同的算法!您的答案是有效的,但对于任意大的值来说是无效的,因为您依赖于最大整数值。这个问题,实际上是一个数字一个数字地工作,而不是仅仅以那种方式显示,更为一般。…。@B.Go Python中没有最大整数值(取决于内存),这让我想知道OP为什么要这样做。@MarkTolonen好的,谢谢你的信息。但我仍然认为,使用与问题相同的算法会更好,并且应该修复该算法,而不是使用不同的算法!