Advertisement
jk464

Sieve of Eranthoses [Threaded] {WIP}

Mar 7th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import datetime
  2. import threading
  3.  
  4. thread_count=4
  5.  
  6. #sieve of eranthoses
  7.  
  8. #function to create intial sieve
  9. def numList(upTo):
  10.     primes = []
  11.     for i in range(upTo):
  12.         primes.append(True)
  13.     return primes
  14.  
  15. primes=numList(10)
  16.  
  17. def dePrime(lower,upper,prime,num):
  18.     i=lower+lower%prime
  19.     global primes
  20.     while i<upper:
  21.         print(stg(num)+" "+str(i))
  22.         primes[i]=False
  23.         i=i+1
  24.  
  25. def thread_dePrime(num_threads,prime):
  26.     global primes
  27.  
  28.     chunksize=int(len(primes)/num_threads)
  29.  
  30.     threads=[]
  31.  
  32.     for i in range(num_threads):
  33.         if i==num_threads:
  34.             threads.append(threading.Thread(target=dePrime, args=(chunksize * (i), len(primes), prime, i)))
  35.         else:
  36.             threads.append(threading.Thread(target=dePrime, args=(chunksize*(i),chunksize*(i+1),prime, i)))
  37.  
  38.     for i in range(num_threads):
  39.         threads[i].start()
  40.  
  41.     for i in range(num_threads):
  42.         threads[i].join()
  43.  
  44. #function to perform the sieving
  45. def primeCalc(primes):
  46.     primes[1] = False #1 is not prime
  47.     prime_counter = 0 #intializing prime counter
  48.  
  49.     for i in range(len(primes)): #for loop to go through the array of numbers to test
  50.         if i==0: #skip 0, count array from 1
  51.             print("")
  52.         else:
  53.             if primes[i]: #if the ith element is true then this is a prime
  54.                 #print(str(i)+" is prime")
  55.                 thread_dePrime(thread_count,i)
  56.  
  57.     print(prime_counter)
  58.  
  59. startTime=datetime.datetime.now()
  60.  
  61. primeCalc(primes)
  62.  
  63. endTime=datetime.datetime.now()
  64.  
  65. print("------------------------")
  66. print("Time to Commute: "+str(endTime-startTime))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement