Perfect Number (original) (raw)
Last Updated : 23 Jul, 2025
A number is a perfect number if it is equal to the sum of its proper divisors, that is, the sum of its positive divisors excluding the number itself. Find whether a given positive integer n is perfect or not.
**Examples:
**Input: n = 15
**Output: false
**Explanation: Divisors of 15 are 1, 3 and 5. Sum of divisors is 9 which is not equal to 15.**Input: n = 6
**Output: true
**Explanation: Divisors of 6 are 1, 2 and 3. Sum of divisors is 6.
Table of Content
- [Naive Approach] Divisor Sum Method - O(n) Time and O(1) Space
- [Expected Approach] Optimized Divisor Search Method - O(sqrt n) Time and O(1) Space
[Naive Approach] Divisor Sum Method - O(n) Timeand O(1)Space
A simple Solutionis 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.
C++ `
#include using namespace std;
// Function to check if the number is perfect bool isPerfect(int n) { int sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { sum += i; } } return sum == n; }
int main() { int n = 15; cout << (isPerfect(n) ?"true" : "false"); return 0; }
C
#include <stdio.h> #include <stdbool.h>
// Function to check if the number is perfect bool isPerfect(int n) { int sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { sum += i; } } return sum == n; }
int main() { int n = 15; printf(isPerfect(n) ? "true" : "false"); return 0; }
Java
public class GFG {
// Function to check if the number is perfect
public static boolean isPerfect(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum == n;
}
public static void main(String[] args) {
int n = 15;
System.out.println(isPerfect(n) ? "true" : "false");
}}
Python
Function to check if the number is perfect
def isPerfect(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
if name == 'main': n = 15 print("true" if isPerfect(n) else "false")
C#
using System;
class GFG { // Function to check if the number is perfect static bool isPerfect(int n) { int sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { sum += i; } } return sum == n; }
static void Main() {
int n = 15;
Console.WriteLine(isPerfect(n) ? "true" : "false");
}}
JavaScript
// Function to check if the number is perfect function isPerfect(n) { let sum = 0; for (let i = 1; i < n; i++) { if (n % i === 0) { sum += i; } } return sum === n; }
// Driver Code let n = 15; console.log(isPerfect(n) ? "true" : "false");
`
[Expected Approach] Optimized Divisor Search Method - O(sqrt n) Timeand O(1)Space
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' .
C++ `
// C++ program to check if a given number is perfect // or not #include using namespace std;
// Returns true if n is perfect 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;}
int main() { int n = 15; cout <<(isPerfect(n) ?"true" : "false");
return 0;}
C
#include <stdio.h> #include <stdbool.h>
// Returns true if n is perfect 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;}
int main() { int n = 15; printf("%s", isPerfect(n) ? "true" : "false");
return 0;}
Java
public class GFG {
// Returns true if n is perfect
public 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
return sum == n && n != 1;
}
public static void main(String[] args) {
int n = 15;
System.out.println(isPerfect(n) ? "true" : "false");
}}
Python
def isPerfect(n):
# To store sum of divisors
sum = 1
# Find all divisors and add them
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
if i * i != n:
sum += i + n // i
else:
sum += i
# If sum of divisors is equal to
# n, then n is a perfect number
return sum == n and n != 1if name == 'main': n = 15 print("true" if isPerfect(n) else "false")
C#
using System; class GfG {
// Returns true if n is perfect
public 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 += i + n / i;
else
sum += i;
}
}
// If sum of divisors is equal to
// n, then n is a perfect number
return sum == n && n != 1;
}
public static void Main() {
int n = 15;
Console.WriteLine(IsPerfect(n) ? "true" : "false");
}}
JavaScript
// Returns true if n is perfect function isPerfect(n) {
// To store sum of divisors
let sum = 1;
// Find all divisors and add them
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
if (i * i !== n)
sum += i + n / i;
else
sum += i;
}
}
// If sum of divisors is equal to
// n, then n is a perfect number
return sum === n && n !== 1;}
// Driver program let n = 15; console.log(isPerfect(n) ? "true" : "false");
`
**Interesting facts About Perfect Number
- Every even perfect number is of the form 2pā1ā (2pā1) , where 2pā1 is a prime number.
- It is unknown whether there are any odd perfect numbers.