NumberUtil.isPrimes优化判断(pr#4058@Github)

This commit is contained in:
Looly
2025-09-08 11:49:03 +08:00
parent d48e793cf1
commit fb963439e2
2 changed files with 13 additions and 5 deletions

View File

@@ -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

View File

@@ -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;
}