Perfect Number Program in C

What-is-a-Perfect-Number.jpg

Have you ever thought if a number could be perfectly balanced, i.e., the number is equal to the sum of its own divisors except itself? That special kind of number is called a Perfect Number, and it is one of the coolest ideas in math. They are rare and only a few of them have ever been found. Perfect Numbers are also used in many real-life applications.

In this article, we will learn about Perfect Numbers in detail.

Table of Contents:

What is a Perfect Number?

A Perfect Number is a kind of whole number in mathematics that is exactly equal to the sum of all its proper divisors, i.e., all the numbers that divide it perfectly, excluding the number itself.

In short, we can say that a perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself.

For example,

Take the number 6. The divisors of 6 are 1, 2, and 3. Now, adding all the proper divisors of 6, we get

1 + 2 + 3 = 6
What is a Perfect Number
Master DSA Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

What are the First 4 Perfect Numbers?

The first 4 perfect numbers are:

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 + ... + 4064

These numbers grow very fast and are rare; all known perfect numbers so far are even and follow a special formula.

Which is the Smallest Perfect Number?

From the above list of the first 4 perfect numbers, we see that 6 is the smallest perfect number.

History of Perfect Numbers

The idea of perfect numbers was found thousands of years ago, in ancient Greece.

  • Pythagoras, around the 6th century BCE, a famous Greek mathematician, and his followers found that the numbers had deep meaning and harmony. They considered the perfect numbers to be divine or special.
  • Later, Euclid in around 300 BCE, one of the most famous mathematicians in history, gave a mathematical formula to find perfect numbers, and this idea is still used today.

Mersenne Prime

Marin Mersenne, a French mathematician from the 17th century, studied about the numbers in detail, i.e., a Mersenne prime is a special kind of prime number that has a very specific shape which can be written in the form of,

M = 2ⁿ − 1

where,

  • n is a whole number (a positive integer)
  • M is a prime number

Note: Every number in the form 2ⁿ − 1 is not a prime number. M is only prime if n is prime, but even then, it is not always true.

For example, let’s take n = 4,

2⁴ − 1 = 16 − 1 = 15

15 is not a prime number, because it is divisible by 3 and 5 also. Hence, only some values of n give Mersenne primes.

Mersenne primes are connected to the perfect numbers. Because Euclid’s Perfect Number Theorem only works when 2ⁿ − 1 is a Mersenne prime

Euclid’s Perfect Number Theorem

The great Greek mathematician Euclid, called the father of geometry around 300 BCE, made a theorem that still helps our understanding of perfect numbers today. Euclid found a mathematical formula that can be used to generate certain perfect numbers. But his method only produces an even perfect number, and only under a special condition.

If 2ⁿ − 1 is a prime number (called a Mersenne prime), then the number 2ⁿ⁻¹ (2ⁿ − 1) is a perfect number.

Now, let us learn how to calculate a perfect number using Euclid’s theorem.

1. Pick any number n

2. Then, check whether the number 2ⁿ − 1 is a prime number or not;

3. If it is, then use the formula, 2ⁿ⁻¹ × (2ⁿ − 1)

4. The result will be a perfect number.

Note: If the number is not prime, then you cannot use the formula.

For example, let the number be 2. Then,

2² − 1 = 3

3 is a prime number. Now, moving forward as per the formula.

2¹ × (2² − 1) = 2 × 3 = 6

So, 6 is a perfect number.

Get 100% Hike!

Master Most in Demand Skills Now!

Method to Check for a Perfect Number By Checking Proper Divisors

This method is good for small numbers and works well if you are checking whether a specific number is perfect.

  1. Write down all the proper divisors of the number, excluding the number itself.
  2. Add all the numbers up to all those divisors. If the sum of all the divisors is equal to the original number, then it is a perfect number.

For example, let’s check for the number 28

All proper divisors of 28 are 1, 2, 4, 7, and 14. And sum of all the divisors of 28 is: 1 + 2 + 4 + 7 + 14 = 28

As the sum of all the divisors of 28 is equal to the original number, hence, 28 is perfect.

What is a Perfect Number

Note: This method is best for only small numbers, like under 10,000

Perfect Number Table

Now, let us discuss some commonly known perfect numbers.

Perfect Numbers List from 1 to 100

There are mainly 2 perfect numbers present between 1 and 100.

Perfect Number Proper Divisors Sum of Divisors
6 1, 2, 3 1 + 2 + 3 = 6
28 1, 2, 4, 7, 14 1 + 2 + 4 + 7 + 14 = 28

List of 52 Perfect Numbers

Below is the list of all 52 perfect numbers:

S. No. Perfect Number (Truncated) Number of Digits
161
2282
34963
481284
533,550,3368
68,589,869,05610
7137,438,691,32812
8230,584…952,12819
9265,845…842,17637
10191,561…169,21654
11131,640…728,12865
12144,740…152,12877
13235,627…646,976314
14141,053…328,128366
15541,625…291,328770
16108,925…782,5281,327
17994,970…915,7761,373
18335,708…525,0561,937
19182,017…377,5362,561
20407,672…534,5282,663
21114,347…577,2165,834
22598,885…496,5765,985
23395,961…086,3366,751
24931,144…942,65612,003
25100,656…605,37613,066
26811,537…666,81613,973
27365,093…827,45626,790
28144,145…406,52851,924
29136,204…862,52866,530
30131,451…550,01679,502
31278,327…880,128130,100
32151,616…731,328455,663
33838,488…167,936517,430
34849,732…704,128757,263
35331,882…375,616841,842
36194,276…462,9761,791,864
37811,686…457,8561,819,050
38955,176…572,7364,197,919
39427,764…021,0568,107,892
40793,508…896,12812,640,858
41448,233…950,52814,471,465
42746,209…088,12815,632,458
43497,437…704,25618,304,103
44775,946…120,25619,616,714
45204,534…480,12822,370,543
46144,285…253,37625,674,127
47500,767…378,81625,956,377
48169,296…130,17634,850,340
49451,129…315,77644,677,235
50109,200…301,05646,498,850
51110,847…207,93649,724,095
52388,692…008,57682,048,640

Why only 52 perfect numbers?

Each even perfect number comes from a Mersenne prime, and they are very rare, so far, only 52 of them have been found. That’s why we know only 52 even perfect numbers, but there can be more perfect numbers.

Perfect Numbers Programs in C

The programs below check whether a given number is a perfect number or not by finding its proper divisors that divide it exactly, excluding the number itself. It adds these divisors together, and if their sum is equal to the original number, it is said to be perfect. It uses a for loop and % to check for divisibility. If the sum of the divisors matches the number, it prints that the number is a perfect number; otherwise, it prints that it is not.

C
Perfect Numbers Programs in C

Program to Find Perfect Number in C++, Java, and Python

1. C++ Program to Check a Perfect Number

Below is the C++ program to check for a perfect number:

Cpp

Output:

C++ Program to Check a Perfect Number

2. Java Program to Check a Perfect Number

Below is the Java program to check for a perfect number:

Java

Output:

Java Program to Check a Perfect Number

3. Python Program to Check a Perfect Number

Below is the Python program to check for a perfect number:

Python

Output:

Python Program to Check a Perfect Number

Perfect Numbers vs Perfect Squares

A perfect number is a positive integer that is equal to the sum of its proper divisors and excluding the number itself. Perfect numbers are very rare and grow quickly. Only a few are known, such as 6, 28, 496, and 8128. They are connected to number theory and special prime numbers called Mersenne primes, and also, all known perfect numbers till now are even. For example, 28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and their sum is 28.

On the other hand, a perfect square is a number that results from multiplying an integer by itself. Perfect squares can be either odd or even, and they are closely tied to multiplication and geometry. For example, 16 is a perfect square, which can be written as 4 × 4.

Feature Perfect Number Perfect Square
Meaning Sum of factors Number × itself
How common Rare Very common
Used in Math and primes Shapes and counting
Example 28 16

Applications of Perfect Numbers

1. Mathematics: Perfect numbers are an important part of number theory, as they help us to understand how the numbers are made up of their factors. Mathematicians have studied them for thousands of years because they have interesting properties and patterns. They are also connected to special prime numbers called Mersenne primes.

2. Cryptography: Mersenne prime numbers are used in computer security to protect the information. They are not used directly, but the math behind them helps in finding large prime numbers, which helps in keeping data safe online.

3. Programming: Many perfect number problems are often given in coding interviews, which help beginners to practice using loops, conditions, and efficient algorithms. For example, writing a program to check if a number is perfect or not teaches a person how to deal with the divisors.

4. Digital Signal Processing: In signal processing, perfect numbers have applications, like perfect numbers have been studied about buffer sizes or block alignment in DSP, where symmetrical patterns are main.

Conclusion

Perfect numbers are the numbers that are equal to the sum of their proper divisors, excluding themselves. They are rare, and all the known ones so far are even. These numbers come from Mersenne primes, and no one has ever found an odd perfect number. Perfect numbers are interesting not just in math but also in areas like coding and computer science. They help us learn more about how numbers work and are fun to explore.

If you want to learn more about this topic, you can refer to our DSA Course.

What is a Perfect Number – FAQs

Q1. What are the first 5 perfect numbers?

The first 5 perfect numbers are 6, 28, 496, 8128, and 33550336.

Q2. Is 36 a perfect number?

No, 36 is not a perfect number; it is a perfect square.

Q3. Which number is perfect?

A perfect number is a positive integer whose the sum is equal to its positive proper divisors, excluding the number itself

Q4. Who invented perfect numbers?

Euclid invented the basics of perfect numbers over 2,000 years ago, and he knew that the first four perfect numbers were 6, 28, 496, and 8,128.

Q5. What is the perfect number trick?

The perfect number trick is, find all the proper divisors of the number, add them together, and then if the sum is equal to the original number, then it is a perfect number

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner