Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- import threading
- thread_count=4
- #sieve of eranthoses
- #function to create intial sieve
- def numList(upTo):
- primes = []
- for i in range(upTo):
- primes.append(True)
- return primes
- primes=numList(10)
- def dePrime(lower,upper,prime,num):
- i=lower+lower%prime
- global primes
- while i<upper:
- print(stg(num)+" "+str(i))
- primes[i]=False
- i=i+1
- def thread_dePrime(num_threads,prime):
- global primes
- chunksize=int(len(primes)/num_threads)
- threads=[]
- for i in range(num_threads):
- if i==num_threads:
- threads.append(threading.Thread(target=dePrime, args=(chunksize * (i), len(primes), prime, i)))
- else:
- threads.append(threading.Thread(target=dePrime, args=(chunksize*(i),chunksize*(i+1),prime, i)))
- for i in range(num_threads):
- threads[i].start()
- for i in range(num_threads):
- threads[i].join()
- #function to perform the sieving
- def primeCalc(primes):
- primes[1] = False #1 is not prime
- prime_counter = 0 #intializing prime counter
- for i in range(len(primes)): #for loop to go through the array of numbers to test
- if i==0: #skip 0, count array from 1
- print("")
- else:
- if primes[i]: #if the ith element is true then this is a prime
- #print(str(i)+" is prime")
- thread_dePrime(thread_count,i)
- print(prime_counter)
- startTime=datetime.datetime.now()
- primeCalc(primes)
- endTime=datetime.datetime.now()
- print("------------------------")
- print("Time to Commute: "+str(endTime-startTime))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement