public class test {
public static void main(String[] args) {
int sum=0;
for(int i=2;i<=500;i++){
int j=2;
while(i%j!=0){
j++;
}
if(j==i){
System.out.println(i);
sum+=i;
}
}
System.out.println("素数的和"+sum);
}
}
希望有用!
public class Demo {
private static void sushu(int a) {
// TODO Auto-generated method stub
final int NUMBER_OP = 10;
int mun = 2, total = 0;
while (true) {
if (mun == a)
break;
boolean prime = true;
for (int resdiut = 2; resdiut <= mun / 2; resdiut++) {
if (mun % resdiut == 0){
prime = false;
break;
}
}
if (prime == true) {
total++;
if (total % NUMBER_OP == 0)
System.out.println(mun);
else
System.out.print(mun + " ");
}
mun++;
}
}
public static void main(String[] args) {
Demo.sushu(500);
}
}
运行结果: