1. Introduction
1.介绍
First, let’s go over some basic theory.
首先,我们来看看一些基本理论。
Simply put, a number is prime if it’s only divisible by one and by the number itself. The non-prime numbers are called composite numbers. And number one is neither prime nor composite.
简单地说,如果一个数字只能被1和数字本身整除,那么它就是质数。非质数被称为复合数。而数字1既不是质数也不是合数。
In this article, we’ll have a look at different ways to check the primality of a number in Java.
在这篇文章中,我们将看看在Java中检查一个数字的原始性的不同方法。
2. A Custom Implementation
2.一个自定义的实现
With this approach, we can check if a number between 2 and (square root of the number) can accurately divide the number.
通过这种方法,我们可以检查2和(数字的平方根)之间的数字是否可以准确地除以该数字。
The following logic will return true if the number is prime:
如果数字是质数,下面的逻辑将返回true。
public boolean isPrime(int number) {
return number > 1
&& IntStream.rangeClosed(2, (int) Math.sqrt(number))
.noneMatch(n -> (number % n == 0));
}
3. Using BigInteger
3.使用BigInteger
BigInteger class is generally used for storing large sized integers, i.e., those greater than 64bits. It provides a few useful APIs for working with int and long values.
BigInteger类通常用于存储大尺寸的整数,即大于64bits的整数。它提供了一些有用的API来处理int和long值。
One of those APIs is the isProbablePrime. This API returns false if the number is definitely a composite and returns true if there is some probability of it being prime. It is useful when dealing with large integers because it can be quite an intensive computation to verify these fully.
其中一个API是isProbablePrime。如果数字肯定是复合数,这个API会返回false;如果有一定的可能性是素数,则返回true。在处理大的整数时,它是很有用的,因为要完全验证这些,可能是一个相当密集的计算。
A quick side-note – the isProbablePrime API uses what’s known as “Miller – Rabin and Lucas – Lehmer” primality tests to check if the number is probably prime. In cases where the number is less than 100 bits, only the “Miller – Rabin” test is used, otherwise, both tests are used for checking the primality of a number.
一个快速的旁注 – isProbablePrime API使用所谓的 “Miller – Rabin和Lucas – Lehmer “原始性测试来检查数字是否可能是素数。在数字小于100位的情况下,只使用 “米勒-拉宾 “测试,否则,两种测试都用于检查一个数字的素数。
“Miller-Rabin” test iterates a fixed number of times to determine the primality of number and this iteration count is determined by a simple check which involves the bit length of the number and the certainty value passed to the API:
“Miller-Rabin “测试迭代一个固定的次数来确定数字的首要性,这个迭代次数由一个简单的检查决定,涉及数字的位长和传递给API的确定性值:
public boolean isPrime(int number) {
BigInteger bigInt = BigInteger.valueOf(number);
return bigInt.isProbablePrime(100);
}
4. Using Apache Commons Math
4.使用Apache Commons数学
Apache Commons Math API provides a method named org.apache.commons.math3.primes.Primes, which we will use for checking the primality of a number.
Apache Commons数学API提供了一个名为org.apache.commons.math3.primes.Primes的方法,我们将用它来检查一个数字的原始性。
First, we need to import the Apache Commons Math library by adding the following dependency in our pom.xml:
首先,我们需要通过在pom.xml中添加以下依赖关系来导入Apache Commons Math库。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
The latest version of the commons-math3 can be found here.
最新版本的commons-math3可以在这里找到。
We could do the check just by calling the method:
我们可以通过调用该方法来进行检查。
Primes.isPrime(number);
5. Conclusion
5.结论
In this quick write-up, we have seen three ways of checking for the primality of the number.
在这篇简短的文章中,我们已经看到了三种检查数字的原始性的方法。
The code for this can be found in the package com.baeldung.algorithms.primechecker over on Github.
这方面的代码可以在com.baeldung.algorithms.primechecker Github.上找到。