
public static boolean isprime(int x) ...{
if (x <= 7) ...{
if (x == 2 || x == 3 || x==5 || x == 7)
return true;
}
int c = 7;
if (x % 2 == 0)
return false;
if (x % 3 == 0)
return false;
if (x % 5 == 0)
return false;
int end = (int) Math.sqrt(x);
while (c <= end) ...{
if (x % c == 0) ...{
return false;
}
c += 4;
if (x % c == 0) ...{
return false;
}
c += 2;
if (x % c == 0) ...{
return false;
}
c += 4;
if (x % c == 0) ...{
return false;
}
c += 2;
if (x % c == 0) ...{
return false;
}
c += 4;
if (x % c == 0) ...{
return false;
}
c += 6;
if (x % c == 0) ...{
return false;
}
c += 2;
if (x % c == 0) ...{
return false;
}
c += 6;
}
return true;
}
本文介绍了一种用于判断整数是否为素数的有效算法。该算法首先检查小于等于7的特殊情况,然后通过排除2、3、5的倍数来减少不必要的计算,并使用sqrt(x)作为循环的上限以提高效率。
5154

被折叠的 条评论
为什么被折叠?



