What is wrong with my syntax, and am I doing this efficiently?

I am trying to make a method that tells me the weather or not, is it true or false that the number is prime. here 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 get this error message:

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

I could misinterpret what the error message says, but it seems to me that there is no 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 is not later, it checks if a is divisible by b, if it then returns, if not, then if b is greater than 1, it starts again. if b is not greater than 1, it is also returned.

  • It also seems a bit dirty to make this method two ints that are the same int.

  • / ? , , , int (, int , public static boolean prime proper?

  • , ?

+3
8

prime , . . :

prime (a, b-1) ;

:

return prime (a, b-1) ;

else if (b>1).

, , . , , , , 99 999 999 ?

- , . , , , , , .

, , - , . divide, , ( ).

, , :

  • , 2, 2, 4, 6, 8 0, .
  • , 5, 5, . 60%. , , .

. , , , , , , 9.

10% - , . , . , , 32- , (. ).

:

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

, . , - ( ) , -.

, , - , .

100 , grep, , , : -)


, ( return) , 7 , , ( , , - if (a < 1) ...).

, , prime(3,3).

, , prime(3,2).

, 3 % (2-1) == 0 (N % 1 0).

, false. , , else if (b>2), , , .


, , , . , .

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)) ; 
    }
}
+6

, , , , , return, . . :

return prime(a, b - 1);
+5

b , 1, .

return prime (a, b-1) ;?

+4

, . 2 N? , ?

API, , , , . :

public static boolean prime(int n) {
  return recurse(n, n);
}

private static boolean recurse(int a, int b) {
 ...
}

private , . . - "" , .

. 10 - 5 2; 12 - 6 2; 14 - 7 2; 25. 25 5 5; 9? ? , , , . .

+3

, 7 , , . .

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) 
        {
            // Have to add the return statement
            // here as others have pointed out!
            return prime(a, b-1);
        }
        else
        {
            return true; 
        }

    }

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

, 7.

if(7 == 0) // not true, don't enter this block
else if(7 % 6 == 0) // not true
else if(7 > 1) // true, call prime(7, 6)

if(7 == 0) // not true, don't enter this block
else if(7 % 5 == 0) // not true
else if(6 > 1) // true, call prime(7, 5)

if(7 == 0) // not true, don't enter this block
else if(7 % 4 == 0) // not true
else if(5 > 1) // true, call prime(7, 4)

... prime (7, 2)

if(7 == 0) // not true, don't enter this block

else if(7 % 1 == 0) true, return false

prime(n, 2), false, .

+2

, .

    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) 
    {
        return prime (a, b-1) ;
    }
    else
    {
        return true; 
    }

}

-, .

0

, - return else if (b>1) - , - , b 1, ArithmeticException, a%(b-1) a%0, .

You can avoid this by making the first if statement. if (a == 0 || b == 1) {} This does not improve the way you search for primes, it just makes sure that there is one way to reduce it.

0
source

Similar to @paxdiblo's answer, but a bit more efficient.

public static boolean isPrime(int num) {
    if (num <= 1 || (num & 1) == 0) return false;
    for (int t = 3; t * t <= num; t += 2)
        if (num % t == 0)
            return false;
    return true;
}

Once it is determined that the number is odd, all even numbers can be skipped. This will halve the numbers that need to be checked.

0
source

Source: https://habr.com/ru/post/1736237/


All Articles