Results 1 to 2 of 2
 11112012, 05:13 PM #1Member
 Join Date
 Oct 2012
 Posts
 4
 Rep Power
 0
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
Exception in thread "main" java.lang.StackOverflowError
at SuperPrime.isSuper(SuperPrime.java:62)
at SuperPrime.isSuper(SuperPrime.java:69)

Java Code: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, size1); 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, k1, digits, count); return 1; } }
 11122012, 12:43 PM #2Moderator
 Join Date
 Apr 2009
 Posts
 12,873
 Rep Power
 22
Similar Threads

recursive 99 bottles program is there better way to write it as a beginner?
By darkbandit in forum New To JavaReplies: 8Last Post: 09262012, 10:56 AM 
stackoverflow error
By sushmita_nigam in forum New To JavaReplies: 1Last Post: 07012012, 02:20 PM 
StackOverFlow
By s0meb0dy in forum New To JavaReplies: 1Last Post: 02172011, 07:50 AM 
help with BNF Grammar program using recursive descent parsing
By carolain79@hotmail.com in forum New To JavaReplies: 1Last Post: 10212009, 08:00 PM 
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08022007, 03:08 PM
Bookmarks