I'm using an algorithm with recursion to calculate 30 perfect numbers, but only calculates the first 4 and then the program throws an error.
public class PerfectNumbers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
getPerfectNumbers(1,1);
}
static void getPerfectNumbers(long out,long number) {
long total = 0;
if(out==30) {
return;
}
for ( int i=1;i<number;i++) {
if(number%i==0) {
total+=i;
}
}
if(total==number) {
System.out.println("Perfect Number "+number);
out++;
}
number++;
getPerfectNumbers(out,number);
}
}
What's wrong with the algorithm?