輸出1~100之間的所有質數

/*
輸出1~100之間的所有質數
只能被1和本身整除的數 (即:從2開始到小於這個數本身結束的自然數中,沒有能被這個數除盡的數存在)
*/
class  TestZhiShu
{
    public static void main(String[] args) 
    { 
        boolean flag=true;
        long start=System.currentTimeMillis();//獲取當前時間(ms)
        for(int i=2; i<10000;i++){
          for(int j=2; j<=Math.sqrt(i);j++){
               if(i%j==0){
                  flag=false;
                  break;
               }
                  
          } 
          if(flag==true)
             System.out.println(i);
           flag=true;
        } 
        long end=System.currentTimeMillis();
        System.out.println("程序執行時間爲:"+(end-start)+"ms");
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章