Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- #sieve of eranthoses
- #function to create intial sieve
- def numList(upTo):
- primes = []
- for i in range(upTo):
- primes.append(True)
- return primes
- #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
- prime_counter = prime_counter + 1 #inc prime counter
- #remove multiples of this prime as possible primes
- counter = 2 * i
- # print(counter)
- while counter <= len(primes) - 1:
- primes[counter] = False
- counter = counter + i
- # print(counter)
- print(prime_counter)
- startTime=datetime.datetime.now()
- primeCalc(numList(1000000))
- endTime=datetime.datetime.now()
- print("------------------------")
- print("Time to Commute: "+str(endTime-startTime))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement