. 1 # Coding GBK = 2 COUNT = 0 . 3 A = int (INPUT ( ' Enter the first number: ' )) . 4 B = int (INPUT ( ' enter the second number: ' )) . 5 Print ( ' in that Fibonacci number 1000 in the two-digit Pact begins as: ' , End = ' ' ) . 6 Print (a, B, End = ' ' ) . 7 the while (a + B) <1000 : . 8 COUNT + = . 1 . 9 IF COUNT ==. 1 : 10 A = A + B . 11 Print(A, End = ' ' ) 12 is the else : 13 is A = A + B 14 Print (A, End = ' ' ) 15 B = A - B 16 # ############### ############################# 17 E: \ Python folder \ venv \ Scripts \ python.exe E: / python folder / jiujiu.py 18 is enter the first number: 1 . 19 enter the second number: 1 20 is that within the Fibonacci number 1000 of the lease beginning with two digits as: 112358132134 144 233 377 610 89 55 987 21 is Process Finished with code 0 Exit 22 is # ################################## ########## 23E: \ python folder \ venv \ Scripts \ python.exe E: / python folder / jiujiu.py 24 enter the first number: 1 25 enter the second number: 3 26 is in the double digits starting in 1000 that the Fibonacci number as Qi:. 1. 7. 4. 3. 11 18 is 29 47 76 123 199 322 521 843 27 Process Finished Exit with code 0
Fibonacci series Fibonacci defined as: F (1) = 1, F (2) = 1, F (n) = F (n-1) + F (n-2) (n> = 3, n∈N *), that is based on a number derived by adding the two numbers before and after, for example: 1,1,2,3,5,8,13,21,34,55 ...... then according to this formula, we can be sure that using a cyclic code can do (code lines starting 7), is now that the cycle, because the first two numbers given are not fixed, can be classified as a special case, a counter variable cOUNT is required, when the count = 1 time, i.e. the first two numbers a and b are added first (i.e., 9 to 11 lines of code), and then the rest of the case where there is a pattern, the added value as an addend, summand minus one summand cycle (i.e. a cycle on addend) to begin a new cycle (i.e. the code lines 13 to 15). It is noted here that the determination of the condition code lines 7, where a + b is less than 1000, if it is a less than 1000, then the result is equivalent to more than one cycle, there will be a number appears more than 1,000, not still on.
Look at the code 10-11 and 13-14, both the code is exactly the same, then there is not optimized feasibility of it? In fact, the code can be written in the following form:
. 1 # Coding GBK = 2 A = int (INPUT ( ' Enter the first number: ' )) . 3 B = int (INPUT ( ' enter the second number: ' )) . 4 C = 0 . 5 Print ( ' in Fibonacci within the two-digit number that begins 1000 deed as: ' , End = ' ' ) . 6 Print (a, B, End = ' ' ) . 7 the while (a + B) <1000 : . 8 C = a + B . 9 Print (C, End = ' ' ) 10 A = B . 11 B = C 12 is # ######################################### 13 is D: \ Untitled \ Project2 \ Venv \ the Scripts \ python.exe D: / Untitled / Project2 / DAY1 / feibonaqi.py 14 enter the first number: 1 15 enter the second number: 4 16 to start the two-digit in 1000 that the Fibonacci number as Qi:. 1. 4. 5. 9 14 97 23 is 37 [60 411 157 254 665 . 17 Process Finished Exit with code 0
May be provided as a variable c to replace the intermediate variables, to be noted here that no special circumstances, and is also relatively more easily understood. For example a = 1, b = 4, then the first time into the cycle, 1 + 4 = 5 <1000, c = a + b = 5, the output c, a = b, a into 4, b = 1 from the initial value c, b 4 from the initial value becomes 5, 4 and 5 and the addend and the augend is just the second cycle, and so on, until the value of a + b is greater than 1000, when the loop terminates.
The above two kinds of code sequence highlighted the problem, for example, a = a + b, b = ab, and a = b, b = c, we can also use the assignment operator to better optimize the code as follows:
. 1 # Coding GBK = 2 A = int (INPUT ( ' Enter the first number: ' )) . 3 B = int (INPUT ( ' enter the second number: ' )) . 4 Print ( ' in the two-digit that Fibonacci number in the beginning of 1000 as Pact: ' , End = ' ' ) . 5 Print (a, End = ' ' ) . 6 the while B <1000 : . 7 Print (B, End = ' ' ) . 8 a, B B =, A + B . 9 # ########################################## ### 10D: \ Untitled \ Project2 \ Venv \ the Scripts \ python.exe D: / Untitled / Project2 / DAY1 / feibonaqi.py . 11 enter the first number: 1 12 is enter the second number: 4 13 is in the double digits that Fibonacci number in the beginning of 1000 as Pact:. 1. 4. 5. 9 14 97 23 is 37 [60 411 157 254 665 14 Process Finished Exit with code 0
Here we should note that a, b = b, a + b This formula, which is the assignment operator, the first operation to the assignment.
. 1 a = 0 2 b =. 1 . 3 a, b = b, a + b . 4 # of this assignment, the first calculation is equivalent to the right. 1 a + b = b = =. 1. 1 + 0 . 5 # and then assigned to a and b , then. 1 = A, B =. 1 . 6 # and this in turn is 7 # ################################ 8 = A B . 9 # case. 1 = A 10 B = A + B . 11 # B =. 1. 1 + 2 =
Examples of expansion: Seeking a Fibonacci first n digits of Number, such as what the first two 8-bit digital 2,6 Fibonacci number Fibonacci sequence that is?
2,6,8,14,22,36,58,94,152……
. 1 # Coding GBK = 2 A = int (INPUT ( ' Enter the first number: ' )) . 3 B = int (INPUT ( ' enter the second number: ' )) . 4 COUNT = int (INPUT ( ' please enter the Fibonacci digits of number you want to look: ' )) 5 IF cOUNT == 1 : 6 Print ( ' the first digit is: ' + str (a)) 7 the else : 8 for the n- in the Range (2, COUNT ): . 9 A, B = B, A + B 10 Print (' First ' + STR (COUNT) + ' median: ' + STR (B)) . 11 # ########################### ################ 12 is D: \ Untitled \ Project2 \ Venv \ the Scripts \ python.exe D: / Untitled / Project2 / DAY1 / feibonaqi.py 13 is enter the first number : 2 14 enter the second number: 6 15 enter the digits you want to find that the deed Fibonacci number column: 8 16 8 median: 94 17 18 Process Finished with Exit code 0 19 # ### ######################################## 20 D: \ Untitled \ project2 \ Venv \ scripts \ python.exe D: / Untitled / Project2 / DAY1 / feibonaqi.py 21 is enter the first number: 2 22 Enter the second number: 6 23 Please enter the Fibonacci number sequence of bits deed you're looking for: 1 24- the first digit is: 2 25 26 Process Finished with Exit code 0
Analysis of the above code, the first number is mandatory, it may be regarded as a special case, independent (i.e. the code lines 5-6). The remaining calculation cycle starts from the second number, where the number of cycles to be noted that the range of, for example, to find the first 8 digits, count = 8, range (2, count) ranges from 2 to 7 cycles six times, the resulting number is 7, then add the number 1, number 7, which is actually the first eight digits.