Ai
1 Star 0 Fork 3

aiobc/Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
IsPrime.java 761 Bytes
一键复制 编辑 原始数据 按行查看 历史
Rafael Chaves 提交于 2020-02-03 02:07 +08:00 . Adopt standard Java project layout (#120)
/**
* Tests whether a number is a prime number or not Time Complexity: O(sqrt(n))
*
* @author Micah Stairs, William Fiset
*/
package com.williamfiset.algorithms.math;
public class IsPrime {
public static boolean isPrime(final long n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long limit = (long) Math.sqrt(n);
for (long i = 5; i <= limit; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(5));
System.out.println(isPrime(31));
System.out.println(isPrime(1433));
System.out.println(isPrime(8763857775536878331L));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiobc/Algorithms.git
git@gitee.com:aiobc/Algorithms.git
aiobc
Algorithms
Algorithms
master

搜索帮助