This code was made to find all the perfect numbers between 1 and 1000. A perfect number, is a number where all of it's divisors add up to that number.
For example:
6
is divisible by 3,2, and 1
and3+2+1 = 6
. Therefore 6
is a perfect number.
public class Perfect {
public static void main(String[] args) {
final int LIMIT = 1000;
boolean isPerfect = false;
int i;
for(i = 2; i < LIMIT; i++) {
isPerfect = isNumPerfect(i);
if(isPerfect) {
System.out.println(i + " is a perfect number");
}
}
}
public static boolean isNumPerfect(int i) {
boolean isPerfect = false;
int sum = 1;
int x;
for(x = 2; x <= i / 2; x++) {
if(i % x == 0) {
sum += x;
}
}
if(sum == i) {
isPerfect = true;
}
return isPerfect;
}
}
What are some ways that I can improve on this code? More specifically, ways to shorten it.