C language classic algorithm example 7: complete number

insert image description here

1. Problem description

1.1. What is perfect number

Perfect numbers, also known as perfect numbers or complete numbers, are some special natural numbers.
The sum (i.e., factor function) of all its true factors (i.e. divisors other than itself) is exactly equal to itself.
A number is said to be "perfect" if it is exactly equal to the sum of its proper divisors.
The first perfect number is 6, the second perfect number is 28, the third perfect number is 496, and the following perfect numbers are 8128, 33550336 and so on.

1.2. Definition of perfect number

A number is said to be "perfect" if it is exactly equal to the sum of its proper divisors. A natural number whose sum is equal to itself is called a perfect number. It is called a perfect number or a complete number.
For example:
the first perfect number is 6, it has divisors 1, 2, 3, 6, except its own 6, the remaining 3 numbers are added, 1+2+3=6.
The second perfect number is 28, which has divisors 1, 2, 4, 7, 14, and 28. Except for its own 28, the remaining 5 numbers are added together, 1+2+4+7+14=28.
The third perfect number is 496, which has divisors 1, 2, 4, 8, 16, 31, 62, 124, 248, and 496. Except for its own 496, the remaining 9 numbers are added together, 1+2+4+ 8+16+31+62+124+248=496.
The following perfect numbers are 8128, 33550336 and so on.

1.3. Description of the problem in this article


The description of the problem at the end of this article
is shown in the following points

  1. Output all perfect numbers within 1-10000.
  2. The sum of all the proper factors of a perfect number (that is, divisors other than itself) is exactly equal to itself.
  3. Six is ​​a perfect number, it has divisors 1, 2, 3, 6, except itself 6, the remaining 3 numbers are added, 1+2+3=6.

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 variables

The variable declaration code is as follows

    // 声明变量
    int i, j, k; 
  • The variables i, j, k are declared.

3.3. Use the for loop to find the complete number of 1-10000

Use the for loop to find the complete number of 1-10000 The code is as follows

	/// <summary>
    /// 使用for循环来求1-10000的完数
    /// </summary>
    /// <returns>无</returns>
    for (i = 1; i < 10000; i++)
    {
    
    
        
    }
  • Use a for loop to find the perfect number from 1-10000.
  • The specific implementation of finding the perfect number from 1-10000 is shown in the following points.

3.4. Variable assignment

The variable assignment code is as follows

 	// 变量赋值, 保证每次循环时sum的初值为0
    int sum = 0;
  • Variable assignment ensures that the initial value of sum is 0 each time the loop is run.
  • The variable assignment is used to complete the solution of the number.
    insert image description here

3.5. Determine whether j is a factor of i

Determine whether j is a factor of i, the code is as follows

		for (j = 1; j < i; j++)
        {
    
    
            /// <summary>
            /// // 判断j是否为i的因子
            /// </summary>
            /// <returns></returns>
            if (i % j == 0)    
            {
    
    
                sum += j;
            }
        }
  • Determine whether j is a factor of i.

3.6. Determine whether the sum of the factor numbers is equal to the original number

The code to judge whether the factor number is equal to the original number is as follows

		/// <summary>
        /// 判断因子数的和是否和原数相等
        /// </summary>
        /// <returns></returns>
        if (sum == i)
        {
    
    
            printf("%d 的因数是: ", i);

            for (k = 1; k < i; k++)
            {
    
    
                if (i % k == 0)
                {
    
    
                    printf("%d ", k);
                }
            }
            printf("\n");
        }
  • Determine whether the sum of the factor numbers is equal to the original number.
  • A number is perfect if the sum of its divisors is equal to the original number.
  • If the sum of the factor numbers is not equal to the original number, then the number is not perfect.

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

insert image description here

6 的因数是: 1 2 3
28 的因数是: 1 2 4 7 14
496 的因数是: 1 2 4 8 16 31 62 124 248
8128 的因数是: 1 2 4 8 16 32 64 127 254 508 1016 2032 4064

请按任意键继续. . .

  • It can be seen that there are a total of four perfect numbers within 1-10000.
  • These perfect numbers are
  • The factors of 6 are: 1 2 3
  • The factors of 28 are: 1 2 4 7 14
  • The factors of 496 are: 1 2 4 8 16 31 62 124 248
  • The factors of 8128 are: 1 2 4 8 16 32 64 127 254 508 1016 2032 4064

3.7. Function modularization for the function of finding the perfect number within 1-10000

  • Use the method of function writing to find the perfect number within 1-10000.

The function modularization code for calculating the perfect number within 1-10000 is as follows.

3.7.1. Function declaration for function modularization for the function of calculating the perfect number within 1-10000

/// <summary>
/// 求完数的函数声明
/// </summary>
void numberPerfect();
  • Declare the function to find the number.

3.7.2. Function definition for function modularization of the function of calculating the perfect number within 1-10000

The function definition code for function modularization of the perfect number within 1-10000 is as follows.

/// <summary>
/// 求完数的函数定义
/// </summary>
void numberPerfect()
{
    
    
    // 声明变量
    int i, j, k;

    /// <summary>
    /// 使用for循环来求1-10000的完数
    /// </summary>
    /// <returns>无</returns>
    for (i = 1; i < 10000; i++)
    {
    
    
        // 变量赋值, 保证每次循环时sum的初值为0
        int sum = 0;

        for (j = 1; j < i; j++)
        {
    
    
            /// <summary>
            /// // 判断j是否为i的因子
            /// </summary>
            /// <returns></returns>
            if (i % j == 0)
            {
    
    
                sum += j;
            }
        }

        /// <summary>
        /// 判断因子数的和是否和原数相等
        /// </summary>
        /// <returns></returns>
        if (sum == i)
        {
    
    
            printf("%d 的因数是: ", i);

            for (k = 1; k < i; k++)
            {
    
    
                if (i % k == 0)
                {
    
    
                    printf("%d ", k);
                }
            }
            printf("\n");
        }
    }

    printf("\n");
}


  • Add the functions of the above points to the function body, as shown in the function definition above.
  • The function of the function is realized: find the perfect number within 1-10000.
  • The way to use for loop.
  • Find the perfect number within 1-10000.
  • First find the number of factors of itself.
  • Finding whether the sum of the factor numbers is equal to itself.

3.7.3. In the main function, call the function numberPerfect to calculate the perfect number within 1-10000

In the main function, the function numberPerfect, which is used to calculate the perfect number within 1-10000, is called. The code is as follows

 	// 调用求完数的函数
    numberPerfect();

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

insert image description here

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>
/// 求完数的函数声明
/// </summary>
void numberPerfect();


4.2, main.c file

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"

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

    // 调用求完数的函数
    numberPerfect();

    system("pause");
    return 0;
}

/// <summary>
/// 求完数的函数定义
/// </summary>
void numberPerfect()
{
    
    
    // 声明变量
    int i, j, k;

    /// <summary>
    /// 使用for循环来求1-10000的完数
    /// </summary>
    /// <returns>无</returns>
    for (i = 1; i < 10000; i++)
    {
    
    
        // 变量赋值, 保证每次循环时sum的初值为0
        int sum = 0;

        for (j = 1; j < i; j++)
        {
    
    
            /// <summary>
            /// // 判断j是否为i的因子
            /// </summary>
            /// <returns></returns>
            if (i % j == 0)
            {
    
    
                sum += j;
            }
        }

        /// <summary>
        /// 判断因子数的和是否和原数相等
        /// </summary>
        /// <returns></returns>
        if (sum == i)
        {
    
    
            printf("%d 的因数是: ", i);

            for (k = 1; k < i; k++)
            {
    
    
                if (i % k == 0)
                {
    
    
                    printf("%d ", k);
                }
            }
            printf("\n");
        }
    }

    printf("\n");
}





V. Summary

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

  1. Output all perfect numbers within 1-10000.
  2. The sum of all the proper factors of a perfect number (that is, divisors other than itself) is exactly equal to itself.
  3. Six is ​​a perfect number, it has divisors 1, 2, 3, 6, except itself 6, the remaining 3 numbers are added, 1+2+3=6.

insert image description here

The article ends here.
I hope that the C language classic algorithm example in this article: complete number.
It 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/126869337