Created
March 14, 2011 06:22
-
-
Save hnh12358/868837 to your computer and use it in GitHub Desktop.
Verify the Ramanujan's 1/pi formula for 1000 digits of pi.
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
##author: Horacio Nunez | |
import decimal, math, functools | |
D = decimal.Decimal | |
C = decimal.getcontext() | |
C.prec = 1024 | |
def multiply(x,y): return C.multiply(x,y) | |
def divide(x,y): return C.divide(x,y) | |
def addition(x,y): return C.add(x,y) | |
def power(x,y): return C.power(x,y) | |
def sqrt(x): return C.sqrt(x) | |
def factorial(n): | |
result = 1 | |
while n > 1: | |
result *= n | |
n = n - 1 | |
return D(result) | |
def Ramanujan(top): | |
def serie(n): | |
num = multiply(factorial(4 * n),addition(D(1103),multiply(D(26390),D(n)))) | |
dem = multiply(power(factorial(n),D(4)),power(D(396),multiply(D(4),D(n)))) | |
return divide(num,dem) | |
Sum = functools.reduce((lambda x,y: addition(x,y)),map(serie,range(0,top))) | |
return multiply(divide(multiply(D(2),sqrt(D(2))),D(9801)),Sum) | |
def diff(str1,str2): | |
if len(str1) != len(str2): | |
return "Strings doesn't have the same length" | |
else: | |
for n in range(0,len(str1)): | |
if str1[n] != str2[n]: | |
return "They differ at " + str(n) + ": " + str1[n] + " != " + str2[n] | |
return "They are equals!" | |
approx_pi = D("""3.14159265358979323846264338327950288419716939937510582097494 | |
4592307816406286208998628034825342117067982148086513282306647 | |
0938446095505822317253594081284811174502841027019385211055596 | |
4462294895493038196442881097566593344612847564823378678316527 | |
1201909145648566923460348610454326648213393607260249141273724 | |
5870066063155881748815209209628292540917153643678925903600113 | |
3053054882046652138414695194151160943305727036575959195309218 | |
6117381932611793105118548074462379962749567351885752724891227 | |
9381830119491298336733624406566430860213949463952247371907021 | |
7986094370277053921717629317675238467481846766940513200056812 | |
7145263560827785771342757789609173637178721468440901224953430 | |
1465495853710507922796892589235420199561121290219608640344181 | |
5981362977477130996051870721134999999837297804995105973173281 | |
6096318595024459455346908302642522308253344685035261931188171 | |
0100031378387528865875332083814206171776691473035982534904287 | |
5546873115956286388235378759375195778185778053217122680661300 | |
1927876611195909216420198""".replace('\n','').replace(' ','')) | |
result = Ramanujan(130); | |
expected_fraction = str(divide(D(1),approx_pi))[:1001] | |
actual_fraction = str(result)[:1001] | |
expected_pi = str(approx_pi) | |
actual_pi = str(divide(D(1),result))[:1001] | |
print("Regarding 1/pi: " + diff(expected,actual)) | |
print("Regarding pi: " + diff(expected_pi,actual_pi)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print("Regarding 1/pi: " + diff(expected,actual))
NameError: name 'expected' is not defined