[問題] 搜尋質數
跑出來結果不對,可以請版上高手指點一下嗎?
感謝嘿!
import java.io.*;
class BooleanType{
// 一維陣列的應用:求質數
public static void main(String args[])
{
final int MAX=300;//Once it is initiated it can not be changed.
//false為質數,true為非質數
//宣告後若沒有給定初值,其預設值為false
boolean prime[]=new boolean[MAX];
prime[0]=true;
prime[1]=true;//0 and 1 are not prime;
int a;
int i;
int count=0;
for(i=2;i<MAX;i++ )
{ for(a=2;a<i;a++)
{
if(i%a==0){
prime[i]=true;
}else{
prime[i]=false;
}
}
}
for(i=2;i<MAX;i++){
if(prime[i]=false)
{
count++;
System.out.println(i);
}
}
System.out.println("the number of prime is "+count);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.160.31.118
→
07/02 17:29, , 1F
07/02 17:29, 1F
※ 編輯: Nozaki 來自: 1.160.31.118 (07/02 17:32)
→
07/02 20:43, , 2F
07/02 20:43, 2F
→
07/02 20:45, , 3F
07/02 20:45, 3F
→
07/02 22:04, , 4F
07/02 22:04, 4F
→
07/03 07:12, , 5F
07/03 07:12, 5F
討論串 (同標題文章)