生活随笔
收集整理的這篇文章主要介紹了
素数的判断,以及素数的遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
去除偶數之后
從3到X-1,每次加 2
public static void main(String
[] args
) {Scanner in
=new Scanner(System
.in
);int []primes
=new int [50];primes
[0]=2;int cnt
=1;MAIN_LOOP
:for(int x
=3;cnt
<50;x
++){for(int i
=0;i
<cnt
;i
++){if(x
%primes
[i
]==0){continue MAIN_LOOP
;}}primes
[cnt
++]=x
;}for(int k
:primes
){System
.out
.print(k
+",");}}
Scanner in
=new Scanner(System
.in
);boolean [] isprime
=new boolean[100];for(int i
=0;i
<isprime
.length
;i
++){isprime
[i
]=true;}for(int i
=2;i
<isprime
.length
;i
++){if(isprime
[i
]){for(int k
=2;i
*k
<isprime
.length
;k
++){isprime
[i
*k
]=false;}}}for(int i
=2;i
<isprime
.length
;i
++){if(isprime
[i
]){System
.out
.print(i
+",");}}}
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的素数的判断,以及素数的遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。