求正整數n以內的所有質數個數并給出計算時間

3年以前  |  閱讀數:153 次  |    
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<n;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");
    }
}

運行結果:

相關文章:

18禁止午夜福利体验区,人与动人物xxxx毛片人与狍,色男人窝网站聚色窝,女生把筷子放屁眼里,国产精品久久久,国产日产欧洲无码视频