How to resolve the algorithm Perfect numbers step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Perfect numbers step by step in the C programming language

Table of Contents

Problem Statement

Write a function which says whether a number is perfect.

A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).

Note:   The faster   Lucas-Lehmer test   is used to find primes of the form   2n-1,   all known perfect numbers can be derived from these primes using the formula   (2n - 1) × 2n - 1. It is not known if there are any odd perfect numbers (any that exist are larger than 102000). The number of   known   perfect numbers is   51   (as of December, 2018),   and the largest known perfect number contains  49,724,095  decimal digits.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perfect numbers step by step in the C programming language

The provided code performs two separate tasks: finding perfect numbers and finding abundant numbers.

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisorsexcluding the number itself. For example, 6 is a perfect number because 6 = 1 + 2 + 3.

An abundant number is a number whose sum of proper divisors is greater than the number itself. For example, 12 is an abundant number because 1 + 2 + 3 + 4 + 6 = 16, which is greater than 12.

The first main function in the code finds and prints all perfect numbers between 2 and 33550337. It does this by using a loop to iterate through all numbers in the given range and calling the perfect function to check if each number is perfect. If the perfect function returns true, the number is printed to the console.

The second main function in the code finds and prints all abundant numbers between 2 and 33550337. It does this by using a loop to iterate through all numbers in the given range and calling the get_factors function to get all of the factors of each number. The sum of the factors is then calculated, and if the sum is greater than the number itself, the number is printed to the console.

Here is a breakdown of the code:

  • The perfect function checks if a given number is perfect. It does this by looping through all of the factors of the number and adding them together. If the sum of the factors is equal to the number, the function returns true; otherwise, it returns false.
  • The first main function loops through all of the numbers between 2 and 33550337 and calls the perfect function to check if each number is perfect. If the perfect function returns true, the number is printed to the console.
  • The get_factors function takes a number and a pointer to an array as arguments. It fills the array with all of the factors of the number and returns the number of factors found.
  • The second main function loops through all of the numbers between 2 and 33550337 and calls the get_factors function to get all of the factors of each number. The sum of the factors is then calculated, and if the sum is greater than the number itself, the number is printed to the console.

Source code in the c programming language

#include "stdio.h"
#include "math.h"

int perfect(int n) {
    int max = (int)sqrt((double)n) + 1;
    int tot = 1;
    int i;

    for (i = 2; i < max; i++)
        if ( (n % i) == 0 ) {
            tot += i;
            int q = n / i;
            if (q > i)
                tot += q;
        }

    return tot == n;
}

int main() {
    int n;
    for (n = 2; n < 33550337; n++)
        if (perfect(n))
            printf("%d\n", n);

    return 0;
}


int main()
{
	int j;
	ulong fac[10000], n, sum;
 
	sieve();
 
	for (n = 2; n < 33550337; n++) {
		j = get_factors(n, fac) - 1;
		for (sum = 0; j && sum <= n; sum += fac[--j]);
		if (sum == n) printf("%lu\n", n);
	}
 
	return 0;
}


  

You may also check:How to resolve the algorithm Pangram checker step by step in the Ruby programming language
You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the TutorialD programming language
You may also check:How to resolve the algorithm Function composition step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Python programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Delphi programming language