Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class e521{
- private static long tot=500000000000L;
- private static boolean[] Erast=new boolean[1000000];
- private static int[] primi=new int[78498];
- public static synchronized void add(int i){
- tot+=i;
- }
- public static void main(String[] args) throws InterruptedException{
- Tasker t1;
- Tasker t2;
- Tasker t3;
- Tasker t4;
- int y=0;
- int j;
- int multiple;
- for (j = 2; j<1000000; j++) {
- if (!Erast[j]) {
- primi[y++]=j;
- multiple = j * 2;
- while (multiple < 1000000) {
- Erast[multiple] = true;
- multiple += j;
- }
- }
- }
- System.out.println("Done Erast " + tot);
- ExecutorService exec=Executors.newFixedThreadPool(4);
- for(int i=3;i<10000000;i+=40000){
- t1=new Tasker(i);
- t2=new Tasker(i+10000);
- t3=new Tasker(i+20000);
- t4=new Tasker(i+30000);
- exec.execute(t1);
- exec.execute(t2);
- exec.execute(t3);
- exec.execute(t4);
- //System.out.printf("t da %d > ",i);
- }
- exec.shutdown();
- try{
- if(exec.awaitTermination(10, TimeUnit.SECONDS))
- System.out.println("Terminated");
- else System.out.println("TimeOut");
- }
- catch(InterruptedException e) {e.printStackTrace();}
- System.out.print(tot);
- }
- public static class Tasker implements Runnable{
- private final int value;
- private final int endValue;
- public Tasker(int i){
- value=i;
- endValue=value+(10000-2);
- }
- public void run(){
- boolean f=true;
- int n=value;
- for(n=value;n<=endValue;n+=2){
- f=true;
- int sqrtN=(int) Math.sqrt(n)+1;
- for(int i=0;primi[i]<sqrtN;i++){
- if(n%primi[i]==0){
- f=false;
- add(primi[i]);
- break;
- }
- }
- if(f) add(n);
- }
- System.out.printf(" Terminated from %d to %d %d\n",value,n, endValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement