这是否正常工作 - Python 3中Fibonacci的总和

时间:2021-05-25 03:59:50

I have a task to make a program that will sum the first 100 Fibonacci numbers. I checked my output in Python, and my output in QBasic 64 and they aren't same. I checked with different inputs also.

我有一个任务是制作一个程序,它将前100个斐波那契数字相加。我在Python中检查了我的输出,在QBasic 64中输出了我的输出并且它们不一样。我也检查了不同的输入。

Input: 10
Output: 89
-----------
Input: 100
Output: 573147844013817084101

Is it correct ?

这是对的吗 ?

Here is my code:

这是我的代码:

n = int(input())
print()

p = 0
d = 1
z = p + d

print(str(p) + ' + ' + str(d) + ' = ' + str(z))

for i in range(n - 2):

    p = d
    d = z
    z = p + d
    print(str(p) + ' + ' + str(d) + ' = ' + str(z))

print('Sum:', z)

EDIT: Code edited again, check it now. I just found on Wikipedia.. It depends from what number you start the loop. So if I use (0, 1, 1, 2, 3, 5, 8, 13, 21, and 34) as first 10 Fibonacci numbers, the sum is going to be 88, not 89.

编辑:再次编辑代码,立即检查。我刚刚在*上找到..这取决于你开始循环的数字。因此,如果我使用(0,1,1,2,3,5,8,13,21和34)作为前10个斐波纳契数,则总和将是88,而不是89。

3 个解决方案

#1


The sums of the first ten and 100 fibonacchi number would be 88 and 573147844013817084100, respectively:

前十和100个斐波纳契数的总和分别为88和573147844013817084100:

>>> cache = {}

>>> def fib(n):
        if n == 0: return 0
        if n == 1: return 1
        if not n in cache:
            cache[n] = fib(n - 1) + fib(n - 2)
        return cache[n]

>>> sum([fib(i) for i in range(10)])
88
>>> sum([fib(i) for i in range(100)])
573147844013817084100

#2


In your loop you are already starting the iteration at the 3rd position, since you set. So set your range to (n -2).

在你的循环中,你已经开始了第三个位置的迭代,因为你设置了。因此,将范围设置为(n -2)。

0: 1 1 : 1 2 : 1 3 : 2 4 : 3 5 : 5

0:1 1:1 2:1 3:2 4:3 5:5

#3


To get the sum of the Fibonacci numbers, using zero as the first in the series, you need to do this:

要获得Fibonacci数的总和,使用零作为系列中的第一个,您需要这样做:

def run_it(n):
    N2 = 0
    N1 = 0
    N = 0
    z = N
    for i in range(n):
        print(N,z)
        N2 = N1
        N1 = N
        if N is 0: N = 1
        else: N = N1 + N2
        z = z + N

run_it(int(input('Number: ')))

To calculate the sum using one as the start of the series, change the initial value of N from zero to one.

要使用一个作为序列的开头来计算总和,请将N的初始值从零更改为1。

#1


The sums of the first ten and 100 fibonacchi number would be 88 and 573147844013817084100, respectively:

前十和100个斐波纳契数的总和分别为88和573147844013817084100:

>>> cache = {}

>>> def fib(n):
        if n == 0: return 0
        if n == 1: return 1
        if not n in cache:
            cache[n] = fib(n - 1) + fib(n - 2)
        return cache[n]

>>> sum([fib(i) for i in range(10)])
88
>>> sum([fib(i) for i in range(100)])
573147844013817084100

#2


In your loop you are already starting the iteration at the 3rd position, since you set. So set your range to (n -2).

在你的循环中,你已经开始了第三个位置的迭代,因为你设置了。因此,将范围设置为(n -2)。

0: 1 1 : 1 2 : 1 3 : 2 4 : 3 5 : 5

0:1 1:1 2:1 3:2 4:3 5:5

#3


To get the sum of the Fibonacci numbers, using zero as the first in the series, you need to do this:

要获得Fibonacci数的总和,使用零作为系列中的第一个,您需要这样做:

def run_it(n):
    N2 = 0
    N1 = 0
    N = 0
    z = N
    for i in range(n):
        print(N,z)
        N2 = N1
        N1 = N
        if N is 0: N = 1
        else: N = N1 + N2
        z = z + N

run_it(int(input('Number: ')))

To calculate the sum using one as the start of the series, change the initial value of N from zero to one.

要使用一个作为序列的开头来计算总和,请将N的初始值从零更改为1。