public 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;
}
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;
}
int y=0;
c: for(int j=2;j<i;j++){
for(int x=2;x<j;x++){
if(j%x==0)
continue c;
}
System.out.println(j);
}
}
记事本写的.格式有点那啥!
最大循环次数 (根号n)/2 ,应该还可以继续优化的。 public boolean isPrime(int number){
int max=number;
if(number%2==0) {
return false;
}
for(int i=3;i<=max;i=i+2){
//count++;
max=number/i;
if(number%i==0){
return false;
}
}
return true;
}