Created
April 4, 2016 06:49
-
-
Save JanneSalokoski/474e3079ec93fc906e56814ce8f5265b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import sys | |
import math | |
def simple(n): | |
series = [] | |
try: | |
for i in range(0, n): | |
series.append( math.pow(-1, i) * (1 / (2*i + 1)) ) | |
result = math.fsum(series) * 4 | |
except Exception as e: | |
return 0 | |
else: | |
return result | |
def ramanujan(n): | |
series = [] | |
try: | |
for i in range(0, n): | |
series.append( (math.factorial(4*i)/math.pow(math.factorial(i), 4)) * (26390 * i + 1103)/(math.pow(396, 4*i)) ) | |
result = (math.sqrt(8)/9801 * math.fsum(series)) * math.pow(math.pi, 2) | |
except Exception as e: | |
return 0 | |
else: | |
return result | |
def main(amount): | |
for i in range(1, amount + 1): | |
print("{:>{}} Simple: {:<20} Ramanujan: {:<20}".format(i, str(len(str(amount))), round(simple(i), 20), round(ramanujan(i), 20))) | |
main(int(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment