A number is a perfect number if is equal to sum of its proper divisors, that is, sum of its positive divisors excluding the number itself. Write a function to check if a given number is perfect or not.
Examples:
Input: n = 15 Output: false Divisors of 15 are 1, 3 and 5. Sum of divisors is 9 which is not equal to 15. Input: n = 6 Output: true Divisors of 6 are 1, 2 and 3. Sum of divisors is 6.
A Simple Solution is to go through every number from 1 to n-1 and check if it is a divisor. Maintain sum of all divisors. If sum becomes equal to n, then return true, else return false.
An Efficient Solution is to go through numbers till square root of n. If a number ‘i’ divides n, then add both ‘i’ and n/i to sum.
Below is the implementation of efficient solution.
C++
// C++ program to check if a given number is perfect // or not #include<iostream> using namespace std; // Returns true if n is perfect bool isPerfect(long long int n) { // To store sum of divisors long long int sum = 1; // Find all divisors and add them for (long long int i=2; i*i<=n; i++) { if (n%i==0) { if(i*i!=n) sum = sum + i + n/i; else sum=sum+i; } } // If sum of divisors is equal to // n, then n is a perfect number if (sum == n && n != 1) return true; return false; } // Driver program int main() { cout << "Below are all perfect numbers till 10000\n"; for (int n =2; n<10000; n++) if (isPerfect(n)) cout << n << " is a perfect number\n"; return 0; } |
Java
// Java program to check if a given // number is perfect or not class GFG { // Returns true if n is perfect static boolean isPerfect(int n) { // To store sum of divisors int sum = 1; // Find all divisors and add them for (int i = 2; i * i <= n; i++) { if (n % i==0) { if(i * i != n) sum = sum + i + n / i; else sum = sum + i; } } // If sum of divisors is equal to // n, then n is a perfect number if (sum == n && n != 1) return true; return false; } // Driver program public static void main (String[] args) { System.out.println("Below are all perfect" + "numbers till 10000"); for (int n = 2; n < 10000; n++) if (isPerfect(n)) System.out.println( n + " is a perfect number"); } } // This code is contributed by mits |
Python3
# Python3 code to check if a given # number is perfect or not # Returns true if n is perfect def isPerfect( n ): # To store sum of divisors sum = 1 # Find all divisors and add them i = 2 while i * i <= n: if n % i == 0: sum = sum + i + n/i i += 1 # If sum of divisors is equal to # n, then n is a perfect number return (True if sum == n and n!=1 else False) # Driver program print("Below are all perfect numbers till 10000") n = 2for n in range (10000): if isPerfect (n): print(n , " is a perfect number") # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# program to check if a given // number is perfect or not class GFG { // Returns true if n is perfect static bool isPerfect(int n) { // To store sum of divisors int sum = 1; // Find all divisors and add them for (int i = 2; i * i <= n; i++) { if (n % i==0) { if(i * i != n) sum = sum + i + n / i; else sum = sum + i; } } // If sum of divisors is equal to // n, then n is a perfect number if (sum == n && n != 1) return true; return false; } // Driver program static void Main() { System.Console.WriteLine("Below are all perfect" + "numbers till 10000"); for (int n = 2; n < 10000; n++) if (isPerfect(n)) System.Console.WriteLine( n + " is a perfect number"); } } // This code is contributed by chandan_jnu |
PHP
<?php // PHP program to check if a given number // is perfect or not // Returns true if n is perfect function isPerfect($n) { // To store sum of divisors $sum = 1; // Find all divisors and add them for ($i = 2; $i * $i <= $n; $i++) { if ($n % $i == 0) { if($i * $i != $n) $sum = $sum + $i + (int)($n / $i); else $sum = $sum + $i; } } // If sum of divisors is equal to // n, then n is a perfect number if ($sum == $n && $n != 1) return true; return false; } // Driver Code echo "Below are all perfect numbers till 10000\n"; for ($n = 2; $n < 10000; $n++) if (isPerfect($n)) echo "$n is a perfect number\n"; // This code is contributed by mits ?> |
Output:
Below are all perfect numbers til 10000 6 is a perfect number 28 is a perfect number 496 is a perfect number 8128 is a perfect number
Time Complexity: √n
Below are some interesting facts about Perfect Numbers:
1) Every even perfect number is of the form 2p?1(2p ? 1) where 2p ? 1 is prime.
2) It is unknown whether there are any odd perfect numbers.
References:
https://en.wikipedia.org/wiki/Perfect_number
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Minimum divisor of a number to make the number perfect cube
- Previous perfect square and cube number smaller than number N
- Number of perfect squares between two given numbers
- Count all perfect divisors of a number
- Program to check Plus Perfect Number
- Check if given number is perfect square
- Minimum digits to remove to make a number Perfect Square
- Find the Next perfect square greater than a given number
- Perfect cube greater than a given number
- Largest factor of a given number which is a perfect square
- Count number less than N which are product of perfect squares
- Largest number in an array that is not a perfect cube
- Check whether the number can be made perfect square after adding 1
- Smallest N digit number which is a perfect fourth power
- Number of edges in a perfect binary tree with N levels

