How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the C++ programming language
How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the C++ programming language
Table of Contents
Problem Statement
These define three classifications of positive integers based on their proper divisors. Let P(n) be the sum of the proper divisors of n where the proper divisors are all positive divisors of n other than n itself.
6 has proper divisors of 1, 2, and 3. 1 + 2 + 3 = 6, so 6 is classed as a perfect number.
Calculate how many of the integers 1 to 20,000 (inclusive) are in each of the three classes. Show the results here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the C++ programming language
This C++ program finds the number of deficient, perfect, and abundant numbers between 1 and 20000.
Here's how the program works:
-
The program includes necessary header files like
<iostream>
,<algorithm>
, and<vector>
. -
The
findProperDivisors
function takes an integern
and finds all its proper divisors (divisors excluding the number itself). It iterates through numbers from 1 ton/2
and checks ifn
is divisible by each number. If so, it adds the divisor to thedivisors
vector. -
In the
main
function:- Three vectors,
deficients
,perfects
, andabundants
, are used to store deficient, perfect, and abundant numbers, respectively. - A loop iterates through numbers from 1 to 20000.
- For each number
n
, it calls thefindProperDivisors
function to find its proper divisors and stores them in thedivisors
vector. - It calculates the sum of the divisors using
std::accumulate
. - Based on the relationship between the sum and
n
, it categorizes the number as deficient, perfect, or abundant. - Deficient numbers have a divisor sum less than
n
, perfect numbers have a divisor sum equal ton
, and abundant numbers have a divisor sum greater thann
.
- Three vectors,
-
Finally, the program prints the count of deficient, perfect, and abundant numbers found within the given range.
Example output:
Deficient : 14892
Perfect : 3
Abundant : 4622
Source code in the cpp programming language
#include <iostream>
#include <algorithm>
#include <vector>
std::vector<int> findProperDivisors ( int n ) {
std::vector<int> divisors ;
for ( int i = 1 ; i < n / 2 + 1 ; i++ ) {
if ( n % i == 0 )
divisors.push_back( i ) ;
}
return divisors ;
}
int main( ) {
std::vector<int> deficients , perfects , abundants , divisors ;
for ( int n = 1 ; n < 20001 ; n++ ) {
divisors = findProperDivisors( n ) ;
int sum = std::accumulate( divisors.begin( ) , divisors.end( ) , 0 ) ;
if ( sum < n ) {
deficients.push_back( n ) ;
}
if ( sum == n ) {
perfects.push_back( n ) ;
}
if ( sum > n ) {
abundants.push_back( n ) ;
}
}
std::cout << "Deficient : " << deficients.size( ) << std::endl ;
std::cout << "Perfect : " << perfects.size( ) << std::endl ;
std::cout << "Abundant : " << abundants.size( ) << std::endl ;
return 0 ;
}
You may also check:How to resolve the algorithm Variables step by step in the C++ programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the Nim programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the beeswax programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Graph colouring step by step in the Python programming language