Data Structure & Algorithms
DSA
Introduction to problem solving
Find Perfect Numbers

# Find Perfect Numbers

## Problem Description

You are given an integer A. You have to tell whether it is a perfect number or not. Perfect number is a positive integer which is equal to the sum of its proper positive divisors. A proper divisor of a natural number is the divisor that is strictly less than the number.

### Problem Constraints

``1 <= A <= 10^6``

### Input Format

``First and only argument contains a single positive integer A.``

### Output Format

``Return 1 if A is a perfect number and 0 otherwise.``

### Example Input

``````Input 1:
A = 4

Input 2:
A = 6``````

### Example Output

``````Output 1:
0

Output 2:
1``````

### Example Explanation

``````Explanation 1:
For A = 4, the sum of its proper divisors = 1 + 2 = 3, is not equal to 4.

Explanation 2:
For A = 6, the sum of its proper divisors = 1 + 2 + 3 = 6, is equal to 6. ``````

### Output

Java
``````public class Solution {
public int isPerfectNumber(int A) {
int sum = 1;
for (int i = 2; i * i <= A; i++) {
if (A % i == 0) {
if (i * i != A) {
sum = sum + i + A / i;
} else {
sum = sum + i;
}
}
}
if (sum == A && A != 1) {
return 1;
}
return 0;
}
}``````
Python
``````class Solution:
def isPerfectNumber(self, A):
if A == 1:
return 0
sum = 1
i = 2
while i * i <= A:
if A % i == 0:
if i * i != A:
sum = sum + i + A // i
else:
sum = sum + i
i += 1
if sum == A:
return 1
return 0``````
JavaScript
``````function isPerfectNumber(A) {
if (A === 1) {
return 0;
}
let sum = 1;
for (let i = 2; i * i <= A; i++) {
if (A % i === 0) {
if (i * i !== A) {
sum = sum + i + A / i;
} else {
sum = sum + i;
}
}
}
if (sum === A) {
return 1;
}
return 0;
}``````