mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
NumberUtil.isPrimes优化判断(pr#4058@Github)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.41(2025-09-06)
|
||||
# 5.8.41(2025-09-08)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 增加`WeakKeyValueConcurrentMap`及其关联类,同时废弃`WeakConcurrentMap`并替换(issue#4039@Github)
|
||||
@@ -14,6 +14,7 @@
|
||||
* 【extra 】 `OsInfo`增加`isWindows11`方法(pr#4054@Github)
|
||||
* 【extra 】 `RedisDS`增加`getPool`和`getSetting`方法(issue#ICVWDI@Gitee)
|
||||
* 【core 】 `NumberUtil.pow`增加重载,支持指数自定义保留位数(pr#4052@Github)
|
||||
* 【core 】 `NumberUtil.isPrimes`优化判断(pr#4058@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复`ReflectUtil`中因class和Method关联导致的缓存无法回收问题(issue#4039@Github)
|
||||
|
||||
@@ -121,6 +121,7 @@ public class NumberUtil {
|
||||
|
||||
/**
|
||||
* 提供精确的加法运算
|
||||
*
|
||||
* @param v1 被加数
|
||||
* @param v2 加数
|
||||
* @return 和
|
||||
@@ -1342,11 +1343,17 @@ public class NumberUtil {
|
||||
*/
|
||||
public static boolean isPrimes(int n) {
|
||||
Assert.isTrue(n > 1, "The number must be > 1");
|
||||
if (n <= 3) return true;
|
||||
if ((n & 1) == 0) return false;
|
||||
if (n % 3 == 0) return false;
|
||||
if (n <= 3) {
|
||||
return true;
|
||||
} else if ((n & 1) == 0) {
|
||||
return false;
|
||||
} else if (n % 3 == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 5; i <= n / i; i += 6) {
|
||||
if (n % i == 0 || n % (i + 2) == 0) return false;
|
||||
if (n % i == 0 || n % (i + 2) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user