How to use Java to determine if a given number is a prime number

Posted by phpstuck on Thu, 23 Sep 2021 21:10:41 +0200

Concerning the definition of prime number: prime number is also called prime number. A natural number greater than 1 is called a prime number if it cannot be divided by any other natural number except 1 and itself; otherwise it is called a total number (specifying that 1 is neither a prime number nor a sum).

Algorithms for generating prime numbers

In our forum, we give an algorithm for generating prime numbers.

This is an interview topic for a company. Please refer to Prime numbers from 1 to 100 (Print prime numbers within 100) Content on the page.

How to determine if a number is a prime number

Why should we judge whether a number is a prime number? Because prime numbers are very important, and the number increases with them, the time complexity of calculation increases, so we need to quickly determine whether a number is a prime number.

You may need to know about the Miller-Rabin primality test.

The Miller-Rabine prime test is a primitive rule that uses a randomization algorithm to determine whether a number is a composite or possibly a prime number. Gary Lee Miller, a professor of computer science at Carnegie Mellon University, first proposed a deterministic algorithm based on the generalized Riemann conjecture, which was later proved by Michael O of the Hebrew University of Jerusalem, Israel.Professor Rabin modified it to present a randomization algorithm independent of this assumption.

Java Native

The following code is a Java native code solution.

        Boolean isPrime = number > 1
                && IntStream.rangeClosed(2, (int) Math.sqrt(number))
                .noneMatch(n -> (number % n == 0));

The code above uses IntStream and uses Math for calculations.

The code above is not very readable, and you probably won't write it that way most of the time.

BigInteger method

We can use BigInteger's isProbablePrime method for approximation.

This approximation uses the Miller-Rabindin test.

This is fine for interviews, because sometimes online code platforms do not provide third-party tools for you to use.

int number = 10;
BigInteger.valueOf(number).isProbablePrime(100);

Apache Math3

This method is very simple, you can use it directly.

It is also the best and fastest method to test.

int number = 10;
Primes.isPrime(number)

Why? That's because Apache's Commons Math3 uses an array to list a range of prime numbers.

Simple and rough, so efficient.

Scope is determined when Java integers do not overflow.

conclusion

Primitives may be used frequently, especially when using random number algorithms.

At the same time, because the algorithm can't cover all the prime numbers, many companies will like to use this topic to help you in interviews.

The complete code is as follows:

    @Test
    public void testIsPrime() {
        int number = 10;
        Boolean isPrime = number > 1
                && IntStream.rangeClosed(2, (int) Math.sqrt(number))
                .noneMatch(n -> (number % n == 0));

        logger.debug(" {} Prime CORE Check is - [{}]", number, isPrime);
        logger.debug(" {} Prime BigInteger Check is - [{}]", number, BigInteger.valueOf(number).isProbablePrime(100));
        logger.debug(" {} Prime APACHE MATH3 Check is - [{}]", number, Primes.isPrime(number));
    }

The output from the above test code is:

15:37:02.403 [main] DEBUG com.ossez.toolkits.codebank.tests.algorithm.PrimeNumbersTest -  10 Prime CORE Check is - [false]
15:37:02.406 [main] DEBUG com.ossez.toolkits.codebank.tests.algorithm.PrimeNumbersTest -  10 Prime BigInteger Check is - [false]
15:37:02.411 [main] DEBUG com.ossez.toolkits.codebank.tests.algorithm.PrimeNumbersTest -  10 Prime APACHE MATH3 Check is - [false]

Process finished with exit code 0

In fact, do we think it's best to be simple and rude?

How to use Java to determine if a given number is a prime number - Java - OSSEZ

Topics: Java Algorithm Interview CWIKIUS