javarecursionruntime-errorstack-overflowperfect-numbers

Error when I use recursion to get more than 4 perfect numbers JAVA


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.

enter image description here

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?


Solution

  • The perfect numbers start by :

    6, 28, 496, 8128, 33550336

    Performing 8128 nested invocations with a method that accepts two long parameter is generally feasible for the JVM.
    I precise "two parameters" as the size of the stack matters in the number of nested invocation accepted by the JVM.
    But from a some level of nested invocation, the JVM throws an Error : java.lang.StackOverflowError that is defined as :

    Thrown when a stack overflow occurs because an application recurses too deeply.

    And 33550336 invocations nested are bound to be too much.

    The algorithm is probably correct but you should favor loop over recursivity to prevent the overflow of the stack.