C language classic algorithm example 6: Fibonacci sequence

insert image description here

1. Problem description

1.1 What is the Fibonacci sequence

The Fibonacci sequence refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...
This sequence starts from the third item, and each item is equal to the previous two Sum.
The definer of the Fibonacci sequence is the Italian mathematician Leonardo Fibonacci (Leonardo Fibonacci), who was born in 1170 AD and died in 1250, and his native place is Pisa.
He is known as the "Leonardo of Pisa".
In 1202 he wrote the book Liber Abacci.
He was the first European to study Indian and Arabic mathematical theories.
His father was hired by a business group in Pisa as a diplomatic consul, stationed in the Algeria region, so Leonardo was able to study mathematics under the guidance of an Arab teacher.
He also studied mathematics in Egypt, Syria, Greece, Sicily, and Provence.
In addition, Fibonacci is also widely used in computer C language programming problems.

  • Fibonacci numbers in nature
    insert image description here

1.2. Description of the problem of the Fibonacci sequence in this paper

The description of the Fibonacci sequence
problem
is as follows

  1. Print the first 20 terms of the Fibonacci sequence.
  2. The 1st and 2nd terms of the sequence are 1 .
  3. Starting from item 3, each item is the sum of the previous 2 items, namely: 1 1 2 3 5 8 .

2. Algorithm example compilation environment

The compilation environment of the C language classic algorithm example in this article uses the integrated development environment: Visual Studio 2019
insert image description here

insert image description here

The link to the official website of Visual Studio 2019 is as follows

Visual Studio 2019 official website link
insert image description here

The features of the Visual Studio 2019 integrated development environment are

  1. Visual Studio 2019 installs the Live Share code collaboration service by default.
  2. New welcome window to help users write code quickly, improved search functionality, general performance improvements.
  3. Visual Studio IntelliCode AI Help.
  4. Better Python virtualization and Conda support.
  5. And support for .NET Core 3.0 projects including WinForms and WPF, etc.

3. The implementation process of the algorithm example

3.1, including the header file

Include the header file code as follows

#pragma once

// 包含头文件
#include <stdio.h>
#include <stdlib.h>
  • The C language header files that will be used include recent years.

3.2. Declare and define variables

The code to declare the variable is as follows.

 // 声明变量。
    long f1, f2, i;

    f1 = f2 = 1;	/*前两项为 1*/

  • The variables f1, f2, i are declared.
  • Find the first two terms f1 and f2 of the Fibonacci sequence and assign the value 1.

3.3. Output prompt information

The output prompt information code is as follows

	// 输出提示信息。
    printf("斐波那契数列(Fibonacci)的前20项有\n\n");
  • When the program is running, output the prompt information related to the Fibonacci sequence.

Press F5 to compile, and the debugging results are as follows.

insert image description here

3.4. Output the values ​​of the first and second items of Fibonacci

The code to output the value of Fibonacci's 1st and 2nd items is as follows

	// 输出斐波那契的第1项和输出第2项。
    printf("%6d%6d", f1, f2);	

  • Outputs the values ​​of the 1st and 2nd terms of Fibonacci.
  • The values ​​of items 1 and 2 of Fibonacci have been previously assigned a value of 1.

Press F5 to compile, and the debugging results are as follows.

insert image description here

insert image description here

3.5. Calculate and output the value of Fibonacci items 3 to 20

Calculate and output the value of Fibonacci items 3 to 20, the code is as follows

	/// <summary>
    /// 计算和输出斐波那契的第3项到第20项。
    /// </summary>
    for (i = 3; i <= 20; i += 2) 
    {
    
    
        for (i = 3; i <= 20; i += 2) 
        {
    
    
            // 计算下两项
            f1 = f1 + f2; 

            f2 = f1 + f2;

            // 每输出十项换行
            if (i % 11 == 0) 
            {
    
    
                printf("\n");
            }

            // 输出f1,f2
            printf("%6d%6d", f1, f2); 
        }
    }
    printf("\n");
  • Outputs the values ​​of the 3rd and 20th terms of Fibonacci.
  • Wraps each output Fibonacci deca term.

Press F5 to compile, and the debugging results are as follows.

insert image description here

  • The effect displayed by Fibonacci can be output correctly.

3.6. Function modularization for Fibonacci functions

  • Write a function for the above program: FibonacciSequence().
  • Can master the simple usage of functions.

3.6.1. Find the function declaration of the first 20 items of the Fibonacci sequence

The function declaration code for finding the first 20 items of the Fibonacci sequence is as follows

/// <summary>
/// 求斐波那契数列的前20项函数声明
/// </summary>
void FibonacciSequence();

  • The function FibonacciSequence() is declared.
  • Find the first 20 terms of the Fibonacci sequence.

3.6.2. Function definition for finding the first 20 items of the Fibonacci sequence

The function definition code for finding the first 20 items of the Fibonacci sequence is as follows

	/// <summary>
/// 求斐波那契数列的前20项的函数定义
/// </summary>
void FibonacciSequence()
{
    
    
    // 声明变量。
    long f1, f2, i;

    f1 = f2 = 1;	/*前两项为 1*/

    // 输出提示信息。
    printf("斐波那契数列(Fibonacci)的前20项有\n\n");

    // 输出斐波那契的第1项和第2项。
    printf("%6d%6d", f1, f2);	

    /// <summary>
    /// 计算和输出斐波那契的第3项到第20项。
    /// </summary>
    for (i = 3; i <= 20; i += 2) 
    {
    
    
        for (i = 3; i <= 20; i += 2) 
        {
    
    
            // 计算下两项
            f1 = f1 + f2; 

            f2 = f1 + f2;

            // 每输出十项换行
            if (i % 11 == 0) 
            {
    
    
                printf("\n");
            }

            // 输出f1,f2
            printf("%6d%6d", f1, f2); 
        }
    }
    printf("\n");
}

  • The function FibonacciSequence() is defined.
  • Find the first 20 terms of the Fibonacci sequence.

3.6.3. Find the function call of the first 20 items of the Fibonacci sequence

  • The calling code of the function of the first 20 items of the Fibonacci sequence is as follows.
 	// 调用求斐波那契数列的前20项的函数
    FibonacciSequence();
    printf("\n");

Press F5 to compile, and the debugging results are as follows.

insert image description here

  • The first 20 lines of Fibonacci can be output correctly.
  • No ten numbers are displayed on a new line.

4. The complete code of the classic algorithm example program

The complete code of the classic algorithm example program is as follows

4.1, main.h file

#pragma once

// 包含头文件
#include <stdio.h>
#include <stdlib.h>


/// <summary>
/// 求斐波那契数列的前20项函数声明
/// </summary>
void FibonacciSequence();



4.2, main.c file

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"


/// <summary>
/// 主函数
/// </summary>
/// <returns>返回0</returns>
int main()
{
    
    
    system("color 3E");

   // 调用求斐波那契数列的前20项的函数
    FibonacciSequence();
    printf("\n");

    system("pause");
    return 0;
}


/// <summary>
/// 求斐波那契数列的前20项的函数定义
/// </summary>
void FibonacciSequence()
{
    
    
    // 声明变量。
    long f1, f2, i;

    f1 = f2 = 1;	/*前两项为 1*/

    // 输出提示信息。
    printf("斐波那契数列(Fibonacci)的前20项有\n\n");

    // 输出斐波那契的第1项和第2项。
    printf("%6d%6d", f1, f2);	

    /// <summary>
    /// 计算和输出斐波那契的第3项到第20项。
    /// </summary>
    for (i = 3; i <= 20; i += 2) 
    {
    
    
        for (i = 3; i <= 20; i += 2) 
        {
    
    
            // 计算下两项
            f1 = f1 + f2; 

            f2 = f1 + f2;

            // 每输出十项换行
            if (i % 11 == 0) 
            {
    
    
                printf("\n");
            }

            // 输出f1,f2
            printf("%6d%6d", f1, f2); 
        }
    }
    printf("\n");
}







V. Summary

The C language classic algorithm example in this article: Fibonacci sequence, the goal to be achieved is as follows

  1. Print the first 20 terms of the Fibonacci sequence.
  2. The 1st and 2nd terms of the sequence are 1 .
  3. Starting from item 3, each item is the sum of the previous 2 items, namely: 1 1 2 3 5 8 .

insert image description here

The article ends here.
I hope that the C language classic algorithm example in this article: Fibonacci sequence
can inspire your love for C language and algorithm learning.

  • Your support is my greatest encouragement.

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/126837493