Skip to content

Instantly share code, notes, and snippets.

@tikipatel
Created March 15, 2016 20:08
Show Gist options
  • Save tikipatel/a86c8231cc4a91365ceb to your computer and use it in GitHub Desktop.
Save tikipatel/a86c8231cc4a91365ceb to your computer and use it in GitHub Desktop.
Calculating value of pi using prime numbers
from operator import mul
def sievePrimes(maximum):
primes = dict.fromkeys(range(3,maximum+1,2),True)
sieveList = [2]
for num in sorted(primes):
if primes[num] == True:
sieveList.append(num)
j = num ** 2
while j < maximum:
if j in primes:
primes[j] = False
j += num
return(sieveList)
y = sievePrimes(100000)
y.remove(2) # 2 isn't in the list
y = map(lambda x: (1.0+1.0/x) if (x-1)%4==0 else (1.0-1.0/x),y)
two_over_pi = (reduce(mul, y,1))
print(["2/(PI) is approximately " , two_over_pi])
print(2.0/two_over_pi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment