// 変な数字は考慮しない
class PrimeMT{
public int num;
public boolean[] primeData;
public boolean[] primeDataSolved;
public PrimeMT(int n){
num=n;
initialize();
solve();
for (int i=-8;i<=8;i++)
System.
out.
println("isPrime("+(num
/2+i
)+") -> "+isPrime
(num
/2+i
)); }
public static void main
(String[] args
){ new PrimeMT(1000100);
}
public void initialize(){
primeData = new boolean[num]; // 全てfalse
primeDataSolved = new boolean[num]; // 全てfalse
primeData[0]=false; // ?
primeData[1]=false;
primeData[2]=true;
}
public void solve(){
Thread t1
=new SolveThread
(this); Thread t2
=new SolveThread
(this); Thread t3
=new SolveThread
(this); Thread t4
=new SolveThread
(this); try{
t1.start();t2.start();t3.start();t4.start();
t1.join();t2.join();t3.join();t4.join();
e.printStackTrace();
}
}
public boolean isPrime(int i){
if (i<0 || num<=i) return false;
return primeData[i];
}
public synchronized int getNext(int offset){
for (;offset<num;offset++)
if (!primeDataSolved[offset]){
primeDataSolved[offset]=true;
return offset;
}
return -1;
}
}
class SolveThread
extends Thread{ PrimeMT prime;
int current;
int num;
public SolveThread(PrimeMT p){
super();
prime=p;
num=p.num;
current=3;
}
public void run(){
while ((current=prime.getNext(current))!=-1){
int j;
for (j=2;j*j<current;j++)
if (current%j==0) break;
if (current%j!=0){
prime.primeData[current]=true;
for (j=1;j*current<num;j++)
prime.primeDataSolved[j*current]=true;
}
}
}
}