Fibonacci (Fibonacci) sequence

This array has the following characteristics:

The first and second numbers are 1 and 1. Starting from the third number, this number is the sum of the previous two numbers. That is, the sequence is 1,1,2,3,5,8,13...

Use for loop to write f1=1, f2=1, f3=f1+f2, as follows;

efdb2e4c41ea488eae811068b58922b0.png

 The above is written with simple variables, only two variables are defined, the program can calculate and output each number sequentially, but cannot save these numbers in memory. If you want to directly output the 25th number in the sequence, it is very difficult. If you use an array to deal with it, it is conceptually simpler:

Each array element represents a number in the array, and each number is calculated in turn and stored in the corresponding array element.

For example, the first 20 Fibonacci written below

0b5afae2a24d4e28bd2658f9b67b9f63.png

 

Guess you like

Origin blog.csdn.net/m0_73939236/article/details/128019871
Recommended