# Recursive program keeps saying Stackoverflow HELP :(

• 11-11-2012, 05:13 PM
zygmfb
Recursive program keeps saying Stackoverflow HELP :(
can someone please help me? This program works, but is returning a "StackOverflow" error and I tried all the ways I could, but I can't fix it. I've spent over 15 hours on the program and I badly need help. Here's what it's supposed to do:

------------------------------------

SuperPrime:

2939 is prime, and
293 is prime, and
29 is prime, and
2 is prime,
A superprime number is a prime number with this additional quality: all of the numbers formed by repeatedly chopping off the last digit of the number are also prime. For example, if

2939 is prime, and
293 is prime, and
29 is prime, and
2 is prime,

then 2939 is a superprime number.
Write a program that prompts for a positive integer and displays all superprime numbers of that length. Display these values in ascending order. Recall that 2 is a prime number, and that 1 is NOT a prime number. At the right is a sample run.
Hint: use recursion to generate all possibilities. Make an isPrime method to test each possibility. *ONLY ONE LOOP MAY BE USED, AND IT'S IN THE ISPRIME METHOD*

How many digits? 4
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
--------------------------------------------------
^^ how it's supposed to run.

how mine runs:
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
```  import java.util.Scanner;   public class SuperPrime   {       public static void main(String [] arg)       {         Scanner x = new Scanner(System.in);                 System.out.print("Enter the number of digits: ");         int i = x.nextInt();                 setLim(i);       }         public static boolean isPrime(int e) //checks if number is prime       {         double sqE = Math.sqrt(e);               if(e == 0 || e == 1 || ((double)e%sqE == 0))             return false;         for(int i = 2; i < sqE+1; i++)         {             if(e%i == 0)               return false;         }         return true;       }             public static void setLim(int size)       {         int upLim = (int)Math.pow(10,size);         int lowLim = (int)Math.pow(10, size-1);                 genNum(lowLim, upLim, size);                 }             public static void genNum(int lowLim, int upLim, int size) //gets lowerlimit, upperlimit, and takes # of digits       {         if(lowLim < upLim)             if(isSuper(lowLim, size, size,0) == 0)               System.out.println(lowLim);         //if(lowLim <upLim && (isSuper(lowLim, size, size,0) != 0))           if(lowLim < upLim)             genNum(lowLim+1, upLim, size);       }             public static boolean isLower(int low, int high)       {         if(low < high)             return true;         else             return false;       }             public static int isSuper(int n, int k, int digits, int count) //checks if superprime, 0 is for counter to see that all digits are checked for prime before returning a number       {         if(k == 0)             return 1;         else if(isPrime(n) == true)             count++;          if(k ==1 && n==2)             count++;         if(count == digits)             return 0;         if(k > 0)             return isSuper(n/10, k-1, digits, count);         return -1;       }   }```