Advertisement
Guest User

Untitled

a guest
May 25th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7.  
  8. public class Main
  9. {
  10.     // Liczba do rozlozenia na czynniki
  11.     public final static BigInteger NUMBER = new BigInteger("10000000");
  12.    
  13.     // Liczba wątków
  14.     public final static int THREAD_COUNT = Runtime.getRuntime().availableProcessors();
  15.    
  16.     // Tablica wątków
  17.     public static Thread[] threads = new Thread[THREAD_COUNT];
  18.    
  19.     // Lista dzielników (czynników pierwszych)
  20.     public static final List<Long> divisorList = Collections.synchronizedList(new ArrayList<Long>());
  21.    
  22.     private static BigInteger p = BigInteger.ONE;
  23.    
  24.     public static void main(String[] args)
  25.     {
  26.         System.out.println("Liczba do rozkładu: " + NUMBER);
  27.         System.out.println("Liczba wątków: " + THREAD_COUNT);
  28.        
  29.         long time = System.nanoTime();
  30.        
  31.         for(int i = 0; i < THREAD_COUNT; i++)
  32.         {
  33.             final int currentThread = i;
  34.            
  35.             threads[i] = new Thread()
  36.             {
  37.                 @Override
  38.                 public void run()
  39.                 {
  40.                     long remainder = 0;
  41.                     long divisionResult = NUMBER.longValue();
  42.                    
  43.                     while(multiplyDivisors() != NUMBER.longValue())
  44.                     {
  45.                         BigInteger currentPrime = nextPrime();
  46.                         remainder = 0;
  47.                        
  48.                         System.out.println("Wątek " + currentThread + " sprawdza " + currentPrime.longValue());
  49.                        
  50.                         while(remainder == 0)
  51.                         {
  52.                             remainder = divisionResult % currentPrime.longValue();
  53.  
  54.                             if(remainder == 0)
  55.                             {
  56.                                 divisionResult = divisionResult / currentPrime.longValue();
  57.                                 divisorList.add(currentPrime.longValue());
  58.                             }
  59.                         }
  60.                     }
  61.                    
  62.                     stopThreads();
  63.                    
  64.                 }
  65.             };
  66.            
  67.             threads[i].start();
  68.         }
  69.        
  70.         for(Thread t: threads)
  71.         {
  72.             try
  73.             {
  74.                 t.join();
  75.             }
  76.             catch (InterruptedException e)
  77.             {
  78.                 e.printStackTrace();
  79.             }
  80.         }
  81.        
  82.         System.out.println("Znalezione dzielniki: " + divisorList);
  83.         if(divisorList.size() == 0)
  84.             System.out.println("Podana liczba jest liczbą pierwszą!");
  85.        
  86.         System.out.println("Czas dzialania programu: " + (System.nanoTime() - time) / 1000000000f + " s");
  87.            
  88.     }
  89.    
  90.     @SuppressWarnings("deprecation")
  91.     protected static void stopThreads()
  92.     {
  93.         for(Thread t: threads)
  94.         {
  95.             try
  96.             {
  97.                 t.stop();
  98.             }
  99.             catch (Exception e)
  100.             {
  101.                 e.printStackTrace();
  102.             }
  103.         }
  104.     }
  105.  
  106.     protected static long multiplyDivisors()
  107.     {
  108.         long result = 1;
  109.         for(Long n : divisorList)
  110.             result *= n.longValue();
  111.  
  112.         return result;
  113.     }
  114.    
  115.     public synchronized static BigInteger nextPrime() {
  116.         p = p.nextProbablePrime();
  117.         return p;
  118.     }
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement