用java实现输出0-200之间的质数,每行输出6个。
下面是我的代码,请高手指教
int n=0; int i=0;
outer:for(i=0;i<=200;i++)
{
if(i%7==0&&i%4!=0)
continue outer;
}
System.out.print(" "+i);
n++;
if(n<5)
continue;
System.out.println();
n=0;
}
}
下面是我的代码,请高手指教
int n=0; int i=0;
outer:for(i=0;i<=200;i++)
{
if(i%7==0&&i%4!=0)
continue outer;
}
System.out.print(" "+i);
n++;
if(n<5)
continue;
System.out.println();
n=0;
}
}
public static void main(String[] args) {
int n = 1;
int i = 0;
System.out.print(" " + 2);
for (i = 3; i <= 200; i += 2) {
if (isprime(i)) {
System.out.print(" " + i);
n++;
if (n < 6)
continue;
System.out.println();
n = 0;
}
}
} public static boolean isprime(int x) {
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0)
return false;
}
return true;
}
}
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;
}
total+=i
就可以了;