-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathperfect-number.java
More file actions
37 lines (30 loc) · 811 Bytes
/
Copy pathperfect-number.java
File metadata and controls
37 lines (30 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
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.
*/
public class MyClass {
public static void main(String args[]) {
System.out.println(isPerfect(6));
}
public static boolean isPerfect(int n)
{
int sum = 1;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
sum=sum+i+(n/i);
}
}
System.out.println(sum+" "+n);
return sum==n;
}
}