Java – my syntax is wrong. Is it effective for me to do this?

I'm trying to make a way to tell me whether the weather is a number or a prime This is 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 receive this error message:

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

I may misinterpret the contents of the error message, but in my opinion, since I have a return statement for every possible condition, there are no missing return statements If a is 0, then it returns false. If not, then it checks whether a can be divided by B. If yes, then it returns. If not, then restart if B is greater than 1 If B is not greater than 1, it will also return

>In addition, it seems a bit confusing to make this method take two integers that are the same int. > What's wrong with my syntax / why do I receive an error message? Is there any way to make it so that the method I use in main only needs an int (maybe another method splits the int into two clones and passes it to public static Boolean primeproper? > or is there a more effective method that makes me completely missing?

Solution

In your prime function, there are four possible code paths, one of which does not return anything This is what the error message complains about You need to replace:

prime (a,b-1) ;

yes:

return prime (a,b-1) ;

In the case of else if (b > 1)

Having said that, this is actually not a good way to calculate whether a number is prime The problem is that each recursive call will allocate a stack frame. If you want to find out that 9999999 is a prime number, will you encounter a serious stack overflow problem?

Recursion is a great tool for a subset of problems, but you need to know the stack depth For a more effective solution, you can perform many tests to determine that the number is not a prime, and then check other numbers only with brute force tests

One thing you should pay attention to is to check the separability of smaller numbers first, because it will narrow your search faster And don't use the division made by multiplication. Multiplication is usually faster (although not always)

There may also be some sneaky tricks:

>Except for 2, every number ending in 2, 4, 6, 8 or 0 is non prime. > Every number except 5 is a non - prime number These two rules alone will reduce your search space by 60% Assuming that you take the test number as a string, it is easy to test the last digit of the string even before converting to integer type

There are some more complex rules for feasibility checks If you take a multiple of 9 and add all the numbers to get a new number, then execute the number again, and then continue until you have a number, you will find that it is always 9

This will reduce your search space by another 10%, although it requires more time-consuming checks Remember, these checks are only good for very large numbers For example, the advantage of 32-bit integers is not great, because precomputed bitmaps are more effective there (see below)

For simplicity, I will 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 every time Calculate once, create a digit group of all prime numbers in the range you are interested in (one of the filtering methods will do this), and then just check your value according to the digit group

If you don't even want to calculate the cost of the array every time the program starts, execute once, save the bit array to a disk file, and load it when the program starts

I actually have a list of the first 1 million primes in the file. It's easier and faster to use grep to find out whether a number is prime than to use some code to calculate it: -)

As for why your algorithm (repaired with a return statement) insists that 7 is not a prime number, it will insist that every number is a non prime number (without negative number checking, I'm sure they will cause some serious problems - your first check should be if (a < 1)...) Let's see what happens when you call prime numbers (3,3) The first time it passes, it hits the third condition, so prime (3,2) is called Then it reaches the second condition because 3% (2-1) = = 0 is true (n% 1 is always 0) So it returns false This may be solved by changing the third condition to else, if (b > 2), although I haven't tested it thoroughly, because I don't think a recursive solution is a good idea anyway The following complete code snippet will meet your needs, although I'm glad you want to know what you did wrong This is a sign of someone who really wants to be 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)) ; 
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>