Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:26:13+00:00 2026-05-13T21:26:13+00:00

I’m trying to make a method that will tell me weather or not it

  • 0

I’m trying to make a method that will tell me weather or not it is true or false that a number is prime. here’s the code:

class prime
{

    public static boolean prime (int a, int b) 
    { 
        if (a == 0) 
        {
            return false; 
        }
        else if (a%(b-1) == 0) 
        {
            return false; 
        }
        else if (b>1) 
        {
            prime (a, b-1) ;
        }
        else
        {
            return true; 
        }

    }

    public static void main (String[] arg) 
    {
        System.out.println (prime (45, 45)) ; 
    }
}

when i try to compile this i get this error message:

prime.java:23: missing return statement
    }
    ^
1 error

I could be misinterpreting what the error message is saying but it seems to me that there isn’t a missing return statement since i have a return statement for every possible set of conditions. if a is 0 then it returns false, if it isn’t then it checks to see if a is dividable by b if it is then it returns if not then if b is greater than 1 it starts over again. if b isn’t greater than 1 it also returns.

  • Also it seems a bit messy to have to
    make this method take two ints that
    are the same int.

  • What is wrong with my syntax/ why am i getting the error message? Is there a way to make it so that the method that i use in main only has to take one int (perhaps another method splits that int into two clones that are then passed to public static boolean primeproper?

  • or is there a more effective way of
    going about this that i’m missing
    entirely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:26:13+00:00Added an answer on May 13, 2026 at 9:26 pm

    In your prime function, there are four possible code paths, one of which doesn’t return anything. That is what the error message is complaining about. You need to replace:

    prime (a, b-1) ;
    

    with:

    return prime (a, b-1) ;
    

    in the else if (b>1) case.

    Having said that, this is actually not a good way to calculate if a number is prime. The problem is that every recursive call allocates a stack frame and you’ll get into serious stack overflow problems if you’re trying to work out whether 99,999,999 is a prime number?

    Recursion is a very nice tool for a certain subset of problems but you need to be aware of the stack depth. As to more efficient solutions, the are many tests you can carry out to determine a number is not prime, then only check the others with a brute force test.

    One thing you should be aware of is to check divisibility against smaller numbers first since this will reduce your search scope quicker. And don’t use divide where multiply will do, multiplication is typically faster (though not always).

    And some possibly sneaky tricks along the lines of:

    • every number other than 2 that ends in 2, 4, 6, 8 or 0 is non-prime.
    • every number other than 5 that ends in 5 is non-prime.
      Those two rules alone will reduce your search space by 60%. Assuming you get your test number as a string, it’s a simple matter to test the last digit of that string even before converting to an integral type.

    There are some more complex rules for divisibility checks. If you take a multiple of 9 and sum all the digits to get a new number, then do it again to that number, then keep going until you have a single digit, you’ll find that it’s always 9.

    That will give you another 10% reduction in search space albeit with a more time-expensive check. Keep in mind that these checks are only advantageous for really large numbers. The advantages are not so great for, say, 32-bit integers since a pre-calculated bitmap would be much more efficient there (see below).

    For a simplistic start, I would use the following iterative solution:

    public static boolean prime (int num) {
        int t = 2;
        while (t * t <= num) {
            if ((num % t) == 0) {
                return false;
            }
            t++;
        }
        return true;
    }
    

    If you want real speed in your code, don’t calculate it each time at all. Calculate it once to create a bit array (one of the sieve methods will do it) of all primes across the range you’re interested in, then simply check your values against that bit array.

    If you don’t even want the cost of calculating the array every time your program starts, do it once and save the bit array to a disk file, loading it as your program starts.

    I actually have a list of the first 100 million primes in a file and it’s easier and faster for me to use grep to find if a number is prime, than to run some code to calculate it 🙂


    As to why your algorithm (fixed with a return statement) insists that 7 is not prime, it will insist that every number is non-prime (haven’t checked with negative numbers, I’m pretty sure they would cause some serious problems – your first check should probably be if (a < 1) ...).

    Let’s examine what happens when you call prime(3,3).

    First time through, it hits the third condition so calls prime(3,2).

    Then it hits the second condition since 3 % (2-1) == 0 is true (N % 1 is always 0).

    So it returns false. This could probably be fixed by changing the third condition to else if (b>2) although I haven’t tested that thoroughly since I don’t think a recursive solution is a good idea anyway.


    The following complete code snippet will do what you need although I appreciate your curiosity in wanting to know what you did wrong. That’s the mark of someone who’s actually going to end up a good code cutter.

    public class prime
    {
        public static boolean isPrime (int num) {
            int t = 2;
            while (t * t <= num) {
                if ((num % t) == 0) {
                    return false;
                }
                t++;
            }
            return true;
        }
    
    
        public static void main (String[] arg) 
        {
            System.out.println (isPrime (7)) ; 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.