Java Program to Check if a Given Number is Perfect Number
Last Updated :
30 Jul, 2023
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. In this article, we will learn how to Check Perfect Numbers in Java.
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
So, we basically have to find the sum of the proper divisors of a number.
1. Using Loop to Check Perfect Number in Java
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.
Below is the implementation of the above method:
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");
}
}
Output6 is a perfect number
The complexity of the above method
2. Using Square root to Check Perfect Number in Java
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");
}
}
Output6 is a perfect number
The complexity of the above method
Time Complexity: O(√n)
3. Recursive Approach to Check Perfect Number in Java
Below is the implement the above method:
Java
// Java Program to implement Perfect
// Number using Recursion
import java.util.*;
// Driver Class
public class GFG {
static long sum = 0;
static long isPerfect(long num, int i)
{
// Base Condition
if (i <= num / 2) {
if (num % i == 0) {
sum = sum + i;
}
// after each iteration, increments the value of
// variable i by 1
i++;
// recursive call
isPerfect(num, i);
}
// returns the sum of factors
return sum;
}
// main function
public static void main(String args[])
{
long number = 28, s;
int i = 1;
s = isPerfect(number, i);
// compares sum with the number
if (s == number)
// prints if the s and number are equal
System.out.println(number
+ " is a perfect number");
else
// prints if s and number are not equal
System.out.println(
number + " is not a perfect number");
}
}
Output28 is a perfect number
The complexity of the above method
Time Complexity: O(n)
Similar Reads
Java Program to Check Whether Number is Divisible by 5 Generic rules in mathematics to test divisibility by 5 in a number system is followed as numbers that end in 5 or 0 are divisible by 5. It doesn't matter how big the number is. Modulo operator will be used most frequently in programming when it comes down to divisibility. Knowledge of the range of n
4 min read
Java Program to check Armstrong Number Given a number x. Write a Java Program to determine whether the given number is Armstrong's number or not. In this article, we will learn how to check Armstrong Numbers in Java. What is Armstrong's Number?In a mathematical number system, the Armstrong number is the number in any given number base, w
4 min read
Java Program to Check if count of divisors is even or odd Given a number "n", find its total number of divisors is even or odd. Examples: Input: n = 10 Output: EvenInput: n = 100Output: OddInput: n = 125Output: EvenA naive approach would be to find all the divisors and then see if the total number of divisors is even or odd. The time complexity for such a
4 min read
Perfect Number Program in Java Using While Loop The number which is equal to the sum of its divisors is called a perfect number. Read the entered long number, assigned to the long variable n. While loop iterates until the condition (i<=n/2) is false. If the remainder of n/i=0 then add i value to the sum and increase the i value. After all the
2 min read
Prime Number Program in Java A prime number is a natural number greater than 1, divisible only by 1 and itself. Examples include 2, 3, 5, 7, and 11. These numbers have no other factors besides themselves and one.In this article, we will learn how to write a prime number program in Java when the input given is a Positive number.
6 min read