Home > database >  Basic Java - Finding if a number is a prime number using while loop
Basic Java - Finding if a number is a prime number using while loop

Time:12-25

I have a question regarding an answer that was given here a while ago.
I came up with the same answer myself in the code attached but I'm trying to understand why do I need to divide the input number by 2 (line 10), and not just let the loop run its course till the value of the input number achieved.

 1  import java.util.Scanner;            
 2  public class numIsPrime {
 3      public static void main(String[] args) {
 4          Scanner sc = new Scanner(System.in);
 5          int i = 2; 
 6          boolean isPrime = true;
 7          System.out.println("Enter a number");
 8          int num = sc.nextInt();
 9        
10          while (i < num )   // (i <= num / 2)  
11          {
12              if (num % i == 0) 
13                  isPrime = false; 
14              i  ;
15          }
16       
17          if (isPrime)
18              System.out.println(num   " is a prime number");
19          else // !isPrime
20              System.out.println(num   " isn't a prime number");
21
22      }
23  }

CodePudding user response:

As mentioned in the comments, dividing by 2 is a simplest optimization to reduce the number of checks, however, existing code has a few issues (e.g. returning true for 0 and 1 which are NOT prime numbers) and may be further optimized:

  • break/end the loop as soon as isPrime is set to false
  • skip even numbers by incrementing by 2
  • calculate until i * i <= num
    If this limit is reached, it means that no factor i of num has been found in the range [2, num/i], therefore by definition of the prime numbers, all the remaining numbers in the range [num/i, num] are neither the factors of num, and therefore num is prime.
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = sc.nextInt();

boolean isPrime = num % 2 != 0 || num == 2;
int i = 3;

while (isPrime && i * i <= num) {
    if (num % i == 0) 
        isPrime = false; 
    i  = 2; // skip even numbers
}
if (isPrime)
    System.out.println(num   " is a prime number");
else 
    System.out.println(num   " isn't a prime number");

More optimizations are possible if the divisibles of 3 (except 3) are excluded similar to the exclusion of even numbers, then the search continues from 5 and the candidates for primality comply with 6n ± 1 rule (e.g., 5 = 6 - 1, 7 = 6 1, 11 = 12 - 1, 13 = 12 1, etc.):

boolean isPrime = (num % 2 != 0 || num == 2) && (num % 3 != 0 || num == 3);
int i = 5;
int d = 2;

while (isPrime && i * i <= num) {
    if (num % i == 0) 
        isPrime = false; 
    i  = d;    // check only non-even numbers
    d = 6 - d; // switch 2 to 4 and back to 2
}

CodePudding user response:

This is the simplest way to calculate if an integer n is probably a prime:

public static boolean isPrime (int n) {
    if (n < 2) return false;
    BigInteger bigInt = BigInteger.valueOf(n);
    return bigInt.isProbablePrime(100);
}

You can insert this function call in a loop where you can pass a new number every iteration. I am using the implementation of BigInteger provided by Java to do the calculation, rather than writing my own. UNLESS this is a homework and you are required to write your own algorithm, I would use this solution.

This base method can then be used for calculating other types of prime numbers. A complete answer can be found here.

UPDATE:

The int parameter in BigInteger.isProbablePrime(int) is a measure of the uncertainty that the caller is willing to tolerate. The larger the number, the "slower" it executes (but the more certain it is).

  • Related