Java Program to Check if a Given Number is Perfect Number - GeeksforGeeks

04 Dec, 2020
A number is said to be a perfect number if the sum of its proper divisors ( i.e. all positive divisors excluding the number itself )is equal to that number itself. Aliquot sum is the sum of divisors of a number, excluding the number itself. Hence, a number is a perfect number only if it is equal to its aliquot sum. All known perfect numbers are even. 
Example 1:

n = 9
Proper Divisors of 9 are 1 and 3.
Sum = 1+3 = 4 ≠ 9
⇒ 9 is not a perfect number.

Example 2:

n = 6
Proper Divisors of 6 are 1, 2 and 3.
Sum = 1+2+3 = 6 = 6
⇒ 6 is a perfect number.

Example 3:

n = 28
Proper Divisors of 28 are 1, 2, 4, 7 and 14.
Sum = 1+2+4+7+14 = 28 = 28
⇒ 28 is a perfect number.

Example 4:

n = 15
Proper Divisors of 15 are 1,3 and 5.
Sum = 1+3+5 = 9 ≠ 15
⇒ 15 is not a perfect number.
So, we basically have to find the sum of proper divisors of a number.
Approach 1:  
A Simple Solution is to go through every number from 1 to n-1 and check if it is a divisor and if it is, then add it in the sum variable and at the end check if the sum is equal to the number itself, then it is a perfect number otherwise not.
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)    {        // 1 is not a perfect number        if (n == 1)            return false;          // sum will store the sum of proper divisors        // As 1 is a proper divisor for all numbers        // initialised sum with 1        int sum = 1;          // Looping through the numbers to check if they are        // divisors or not        for (int i = 2; i < n; i++) {                        if (n % i == 0) {                sum += i;            }                    }          // If sum of divisors is equal to        // n, then n is a perfect number        if (sum == n)            return true;          return false;    }      // Driver program    public static void main(String[] args)    {        int n = 6;                // Call isPerfect function to        // check if the number is perfect or not.        if (isPerfect(n))            System.out.println(n + " is a perfect number");        else            System.out.println(                n + " is not a perfect number");    }}
6 is a perfect number
Time Complexity: O(n)
Approach 2:
An Efficient Solution is to go through numbers till the square root of n. 
If i is a divisor then n/i is also a divisor. 
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)    {        // 1 is not a perfect number        if (n == 1)            return false;          // sum will store the sum of proper divisors        // As 1 is a proper divisor for all numbers        // initialised sum with 1        int sum = 1;          // Looping through the numbers to check if they are        // divisors or not        for (int i = 2; i * i <= n; i++) {                        if (n % i == 0) {                                // n is a perfect square                // let's take 25                // we need to add 5 only once                // sum += i + n / i will add it twice                                if (i * i == n)                    sum += i;                else                    sum += i + (n / i);            }        }          // If sum of divisors is equal to        // n, then n is a perfect number                if (sum == n)                        return true;          return false;    }      // Driver program    public static void main(String[] args)    {        int n = 6;                // Call isPerfect function to        // check if the number is perfect or not.        if (isPerfect(n))                        System.out.println(n + " is a perfect number");                else            System.out.println(                n + " is not a perfect number");    }}
6 is a perfect number
Time Complexity: O(√n)
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?