by CodeChum Admin
I am what you call a perfectionist. I always strive for perfection, and I appreciate everyone and everything that is perfect. That is why I have recently acquired an appreciation for perfect numbers! I absolutely need to know which numbers from 1 to 1000 are considered perfect. From what I recall, a perfect number is a positive integer that is equal to the sum of all its divisors other than itself.
Example:
6 is a perfect number because 6 = 1 + 2 + 3
public class Main {
public static void main(String[] args) {
int bound = 1000;
int sum;
for (int i = 2; i <= bound; i++) {
sum = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
System.out.print(i + " ");
}
}
System.out.println();
}
}
Comments
Leave a comment