Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. public class e521{
  6.     private static long tot=500000000000L;
  7.     private static boolean[] Erast=new boolean[1000000];
  8.     private static int[] primi=new int[78498];
  9.    
  10.     public static synchronized void add(int i){
  11.         tot+=i;
  12.     }
  13.     public static void main(String[] args) throws InterruptedException{
  14.         Tasker t1;
  15.         Tasker t2;
  16.         Tasker t3;
  17.         Tasker t4;
  18.         int y=0;
  19.         int j;
  20.         int multiple;
  21.         for (j = 2; j<1000000; j++) {
  22.             if (!Erast[j]) {
  23.                 primi[y++]=j;
  24.                 multiple = j * 2;
  25.                 while (multiple < 1000000) {
  26.                     Erast[multiple] = true;
  27.                     multiple += j;
  28.                 }                
  29.             }
  30.         }
  31.         System.out.println("Done Erast " + tot);
  32.         ExecutorService exec=Executors.newFixedThreadPool(4);
  33.        
  34.         for(int i=3;i<10000000;i+=40000){
  35.             t1=new Tasker(i);
  36.             t2=new Tasker(i+10000);
  37.             t3=new Tasker(i+20000);
  38.             t4=new Tasker(i+30000);
  39.             exec.execute(t1);
  40.             exec.execute(t2);
  41.             exec.execute(t3);
  42.             exec.execute(t4);
  43.             //System.out.printf("t da %d    >    ",i);
  44.            
  45.         }
  46.         exec.shutdown();
  47.         try{
  48.             if(exec.awaitTermination(10, TimeUnit.SECONDS))
  49.                 System.out.println("Terminated");
  50.             else System.out.println("TimeOut");
  51.         }
  52.         catch(InterruptedException e) {e.printStackTrace();}
  53.         System.out.print(tot);
  54.     }
  55.     public static class Tasker implements Runnable{
  56.         private final int value;
  57.         private final int endValue;
  58.         public Tasker(int i){
  59.             value=i;
  60.             endValue=value+(10000-2);
  61.         }
  62.         public void run(){
  63.             boolean f=true;
  64.             int n=value;
  65.             for(n=value;n<=endValue;n+=2){
  66.                 f=true;
  67.                 int sqrtN=(int) Math.sqrt(n)+1;
  68.                 for(int i=0;primi[i]<sqrtN;i++){
  69.                     if(n%primi[i]==0){
  70.                         f=false;
  71.                         add(primi[i]);
  72.                         break;
  73.                     }
  74.                 }
  75.                 if(f) add(n);
  76.             }
  77.             System.out.printf("   Terminated from %d to %d    %d\n",value,n, endValue);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement