简体   繁体   English

在Python中打印斐波那契数的逻辑

[英]Logic on printing Fibonacci numbers in Python

This is been killing me. 这一直在杀我。 When using the following code to print the Fib number, it gives the right output: 使用以下代码打印Fib编号时,它会提供正确的输出:

a = 0
n = 1
while n < 55:
    print(n)
    a,n = n,a+n

but when I change to this: 但是当我更改为:

a = 0
n = 1
while n < 55:
    print(n)
    a = n
    n = a+n

The output is totally different. 输出完全不同。 I've even run it thru pythontutor.com to watch stepping. 我什至通过pythontutor.com运行它来观看步进。

What are I missing. 我缺少什么。

There is a difference in the way variable values are interpreted and assigned between the two code snippets. 在两个代码段之间解释和分配变量值的方式有所不同。

In case of the first snippet, while assigning the value to "n", the new value of a is not used, rather the value of a from the previous iteration is used. 在第一个代码段的情况下,将值分配为“ n”时,不使用a的新值,而是使用前一次迭代的a值。

But, in case of the second snippet, the value of "a" is first updated and then used for the second statement. 但是,在第二个代码段的情况下,“ a”的值将首先更新,然后用于第二个语句。

Let's take an example: 让我们举个例子:

For the first iteration where n is 1, 对于n为1的第一次迭代

First Code Snippet : At the end of the iteration, the value of a will be 1 and value of n also will be 1. (For n = a+n, value of a is considered 0) 第一个代码段 :在迭代结束时,a的值为1,n的值也为1。(对于n = a + n,a的值视为0)

Second Code Snippet : At the end of the iteration, the value of a will be 1 and value of n will be 2. (For n = a+n, value of a is considered 1) 第二代码段 :在迭代结束时,a的值为1,n的值为2。(对于n = a + n,a的值视为1)

The key point to be noted about the Python Comma Operator is that, all the expressions to the right of the assignment operator are evaluated first before the assignments are actually made and this causes the difference in output between the two. 有关Python逗号运算符的要注意的关键点是,在实际进行赋值之前,首先评估赋值运算符右边的所有表达式,这会导致两者之间的输出差异。

Well, the initial assignment 好,最初的任务

 a,n = n,a+n

means 手段

 old_a = a
 a = n
 n = old_a + n # saved a value (old_a) used 

Please, notice that n = old_a + n , not a + n == 2 * n 请注意, n = old_a + n ,而不是a + n == 2 * n

Let's call a_i and n_i the values of a and n at the i-th iteration. 让我们在第i次迭代中将a_in_i称为an的值。 In the first code you are assigning to n_i+1 the value a_i + n_i , instead in the second code you are assignign to n_i+1 the value a_i+n_i+n_i . 在第一个代码中,您将n_i+1的值分配给a_i + n_i ,而在第二个代码中,您将n_i+1的值分配给a_i+n_i+n_i This happen because you are assigning a before n in the second code, while this happen simultaneously in the first one. 发生这种情况是因为您在第二个代码中分配a n之前的值,而在第一个代码中却同时发生了。 To solve this issue save in a temporary variable the old value of n or a . 要解决此问题,请在临时变量中保存na的旧值。

Alternatively, with just a bit of math, you can do the following without saving any temporary variable: 另外,只需一点数学,您就可以执行以下操作而无需保存任何临时变量:

a = 0
n = 1
while n < 55:
    print(n)
    n = a+n
    a = n-a

which is equivalent to: a_i+1 = n_i+1 - a_i = a_i + n_i - a_i = n_i . 等效于: a_i+1 = n_i+1 - a_i = a_i + n_i - a_i = n_i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM