Results 1 to 2 of 2
  1. #1
    zygmfb is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Exclamation 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, 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;
          }
       }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Recursive program keeps saying Stackoverflow HELP :(

    The stack trace from your exception will show you where it is getting stuck.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 09-26-2012, 10:56 AM
  2. stackoverflow error
    By sushmita_nigam in forum New To Java
    Replies: 1
    Last Post: 07-01-2012, 02:20 PM
  3. StackOverFlow
    By s0meb0dy in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 06:50 AM
  4. help with BNF Grammar program using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 10-21-2009, 08:00 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •