從generatePrimes.run(最大範圍)

從最大範圍取得裡面所有質數

==============================

import java.util.ArrayList;


public class generatePrimes {
public ArrayList<Integer> IntAry;
private int temp;//放要刪去的倍數
private int MaxSqrt;//放最大值的平方根

//初始化ArrayList
private void setAry(int num){
IntAry=new ArrayList<Integer>();
for(int i=0;i<num-1;i++){
IntAry.add(i+2);
}
MaxSqrt=(int)(Math.sqrt(num))+1;
}

//run delete method
public void run(int num){
setAry(num);
delete();
}

//刪除質數的倍數
private void delete(){
int judge=0;
for(int i=0;i<MaxSqrt;i++){
temp=IntAry.get(i);
for(int j=0;j<IntAry.size();j++){
if(IntAry.get(j)%temp==0){
if(judge>0)
IntAry.remove(j);
judge++;
}
}
judge=0;

}

}

}


創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()