Updated March 31, 2023
Introduction to Perfect Number C++
Perfect numbers in C++ are those numbers whose value is equal to the sigma of its divisors (excluding its own number). Divisors are the denominators which divide a numerator completely without leaving any reminder. It has unique characteristics in them which make them special, and they are complete and absolute in nature. However, it is a very rare phenomenon, and so far, mathematicians have invented only 51 numbers out of the number range from one up to a maximum limit one can imagine, and the super computer can process.
Logic Behind Perfect Number
There are no traces in history on who has discovered on invented perfect numbers. It is believed that Egyptians had some interest in Perfect numbers, but it was Greeks who did a lot of research on perfect numbers and people like Pythagoras, O’Connor, Robertson took an extensive interest in Perfect numbers.
There is a belief that there is an associated perfect number for every prime number, and Mersenne also discovered the formula.
The formula is:
Where,
- P – Primary number and (2P-1) is Mersenne prime.
- For primary number 2, the perfect number is 6, and Mersenne prime is 3, and for the next Primary number 3, the perfect number is 28, and Mersenne prime is 7 and so on.
Significance of Perfect Number
Though there are several theories floating around on the importance of perfect numbers and their link with primary numbers etc., the importance of perfect numbers and their usage is still unclear.
Some of the hard facts on perfect numbers are:
- Treated like superior over the other numbers.
- Easy to understand, but there is no visible use to it.
- Not capable of solving any mathematical problems.
- Not a great tool in providing solutions in other fields like Business, Economics and Science.
Knowledge and background of Perfect numbers help Mathematicians to improve their data analysis skills and build AI models for various scenarios.
How to Check Perfect Number in C++?
Program steps to find out if a given number is a perfect number or otherwise:
- Accept a number that has to be validated for a perfect number.
- Divide the number by 1 and check whether the division leaves any remainder.
- Since the remainder is zero, the denominator 1 is a perfect divisor and accumulates the divisor in a counter.
- Divide the number by 2 and check the remainder and if the remainder is zero, accumulate the divisor in the counter.
- Repeat the above step from 3 till one number before the accepted number.
- Check the values of the accepted number and the accumulated counter.
- If the values are the same, then the accepted number is a Perfect number; otherwise, it is not.
Program steps to choose a Perfect number from a given range of numbers:
- Accept the first number and last number in the range.
- Start with the first number. Then, check whether it is a perfect number using the steps in the above paragraph. If it is a Perfect number, then display the number.
- Repeat the above step for the next number in the number range.
- Continue the above steps till the last number is in the range.
Examples of Perfect Number C++
Given below are the examples of Perfect Number C++:
Example #1
Find out if a given number is a perfect number or otherwise.
Code:
#include <iostream>
using namespace std;
int main() // Main ... Program starts here
{
int gno = 6; // The number to be checked
int m = 0; // Initialize variables
int total = 0;
cout << "Check whether this number " << gno <<" is Perfect or not " << ":\n";
// Display the Header
for(m=1; m<gno; m=m+1) // For loop start
{
if(gno % m == 0) // Check Remainder = 0
total = total + m; // If so accumulate
}
// Check number and accumulated value
if(total == gno)
cout << "\n" << "YES... The given number is a perfect Number...";
if(total != gno)
cout << "\n" << "Sorry it is not perfect no.... Try some other";
}
Output:
For the given no 6, the result is:
For the given number 27, the result is:
Change in the code int gno = 27; // The number to be checked
For the given number 469, the result is:
Change in the code int gno = 469; // The number to be checked
For the given number 496, the result is:
Change in the code int gno = 496; // The number to be checked
Example #2
Identify perfect numbers in a given range of numbers.
Code:
#include <iostream>
using namespace std;
int main() // Program starts here
{
int first = 1; // First in the range
int last = 10000; // Last in Range
int pcount = 0;
int count = 0; // Initializing all the variables
int totcount = 0;
int j = 0;
int m = 0;
int total = 0;
int pfound = 0;
// Header printing
cout << "Perfect nos in the Range-" << first <<" and " << last << ":\n";
cout << "\n";
for(j=first; j<=last; j=j+1) // Outer FOR loop
{
for(m=1; m<j; m=m+1) // For a given number - inner FOR loop
{
if(j % m == 0) // Check Remainder
total = total + m; // Accumulate
}
if(total == j) // Check
{
pfound = 1; // Yes Perfect number found
pcount = pcount+1; // Display the number
cout << "perfect number: " << pcount << " " <<j <<"\n";
}
total = 0;
count = count + 1;
if (count > 999)
{
totcount = totcount + count; // Display status for every 1000 nos
cout <<"processsed "<< totcount << " Numbers" <<"\n";
count = 0;
} // inner loop ends
} // outer loop ends
if(pfound == 0)
cout << "There in no perfect number between the given range";
// display if no Perfect no found
}
Output:
The result for a range of numbers ( 1 to 10000):
Conclusion
Even though Perfect numbers could not find any applications in the real world, the fundamentals and concepts help Mathematicians to build data models around complex real-life issues and derive insights into the data.
Recommended Articles
This is a guide to Perfect Number C++. Here we discuss the introduction, logic, significance, how to check perfect numbers in C++? and examples. You may also have a look at the following articles to learn more –