[java]代码库package homework;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class 求正整数n以内的所有质数个数 {
public static int check(int n){
int temp,j,re=3;
if(n==2)
re=1;
else {
for (j = 2; j < n; j++) {
temp = n % j;
if (temp != 0)
re = 1;
else {
re = 0;
break;
}
}
}
return re; //re=1则是,=0不是
}
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n;
System.out.println("enter the number");
n=reader.nextInt();
long start=new Date().getTime();
int i,j=0;
for(i=2;i
int re=check(i);
if(re==1)
j+=1;
}
long end=new Date().getTime();
System.out.println(j);
System.out.println("cost time "+(end-start)+"ms");
}
}
[代码运行效果截图]