Created
March 15, 2016 20:08
-
-
Save tikipatel/a86c8231cc4a91365ceb to your computer and use it in GitHub Desktop.
Calculating value of pi using prime numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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