Don't like ads? PRO users don't see any ads ;-)
Guest

PI D=

By: a guest on Aug 9th, 2012  |  syntax: Python  |  size: 3.79 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #CHUDNOVSKY's WITH GMPY
  2.  
  3. import gmpy
  4. import time
  5. #import math
  6. from decimal import *
  7.  
  8. amountiterations = int(raw_input('Enter amount of iterations: '))
  9. places = int(raw_input('Enter amount of precision you want: '))
  10. def Chudnosky(amountiterations,precision):
  11.  
  12.         gmpy.set_minprec(precision)
  13.         k = 0
  14.         total = 0
  15.         while k < amountiterations:
  16.                 gmpy.set_minprec(precision)
  17.                 sign = (-1)**(k%2)
  18.                 middleterm = gmpy.fac(6*k)
  19.                 endterm = 13591409 + 54514*k
  20.                 numerator = sign*middleterm*endterm
  21.                 dfirstterm = gmpy.fac(3*k)
  22.                 dmiddleterm = (gmpy.fac(k))**3
  23.                 dendterm = gmpy.fsqrt(gmpy.mpf(640320)**((6 * k) + 3))
  24.                 denominator = gmpy.mpf(dfirstterm)*gmpy.mpf(dmiddleterm)*dendterm
  25.                 div = gmpy.mpf(numerator) / denominator
  26.                 #print "div: " + str(div)
  27.                 total += div
  28.                 #print "total: " + str(total)
  29.                 k = k + 1
  30.         total *= 12
  31.         return (gmpy.mpf(1)/total)
  32. start_time = time.time()
  33. result = Chudnosky(amountiterations,places)
  34. calc_time = time.time() - start_time
  35. print(result)
  36. f = open("pi.txt","r")
  37. match = ""
  38. correct = 0
  39. for digit in str(result):
  40.         if digit == f.read(1):
  41.                 match = match + "T"
  42.                 if digit != ".":
  43.                         correct = correct + 1
  44.         else:
  45.                 match = match + "F"
  46. print(match)
  47. f.close()
  48. #print "It took " + str(calc_time) + " seconds to calculate pi with " + str(correct) + "/" + str(len(str(result))) + " digits correct."
  49.  
  50. #END FILE
  51. #CHUDNOVSKY's WITH DECIMAL
  52.  
  53.  
  54. import time
  55. import math
  56. from decimal import *
  57.  
  58. digits = int(raw_input('Enter amount of digits: '))
  59.  
  60. def Chudnosky(digits):
  61.         iter = int(digits/10) + 1
  62.         getcontext().prec = digits*2
  63.         total = Decimal(0)
  64.         for k in range(0,iter+1):
  65.                 sign = (-1)**(k%2)
  66.                 middleterm = Decimal(math.factorial(6*k))
  67.                 endterm = Decimal(13591409) + Decimal(54514*k)
  68.                 numerator = sign*middleterm*endterm
  69.                 dfirstterm = Decimal(math.factorial(3*k))
  70.                 dmiddleterm = Decimal((math.factorial(k))**3)
  71.                 base = Decimal(640320)
  72.                 power = Decimal(6)*k + Decimal(3)
  73.                 dendterm = Decimal.sqrt(base**power)
  74.                 denominator = dfirstterm*dmiddleterm*dendterm
  75.                 div = Decimal(numerator) / Decimal(denominator)
  76.                 #print "div: " + str(div)
  77.                 total += Decimal(div)
  78.                 #print "total: " + str(total)
  79.  
  80.         total *= 12
  81.         getcontext().prec = digits
  82.         return (Decimal(1)/Decimal(total))
  83.  
  84.  
  85.  
  86. start_time = time.time()
  87. result = Chudnosky(digits)
  88. calc_time = time.time() - start_time
  89.  
  90.  
  91.  
  92. print(Decimal(result))
  93. f = open("pi.txt","r")
  94. match = ""
  95. correct = 0
  96. for digit in str(result):
  97.         if digit == f.read(1):
  98.                 match = match + "T"
  99.                 if digit != ".":
  100.                         correct = correct + 1
  101.         else:
  102.                 match = match + "F"
  103. print(match)
  104. f.close()
  105. #print "It took " + str(calc_time) + " seconds to calculate pi with " + str(correct) + "/" + str(len(str(result))) + " digits correct."
  106.  
  107. #END FILE
  108. #Nilakantha with decimal
  109. #need to figure out how to get moar precision
  110. #import bigfloat
  111. #import decimal
  112. from decimal import *
  113. import time
  114. start_time = time.time()
  115. x = 1
  116. pi = Decimal(3)
  117. add = True
  118. iterations = int(raw_input('Enter amount of iterations: '))
  119. places = int(raw_input('Enter amount of decimal places you wish to display: '))
  120.  
  121. getcontext().prec = places
  122. while x <= iterations:
  123.         if add == True:
  124.                 pi = pi + Decimal(4)/Decimal((2*x)*(2*x+1)*(2*x+2))
  125.                 #print "add"
  126.         if add == False:
  127.                 pi = pi - Decimal(4)/Decimal((2*x)*((2*x)+1)*((2*x)+2))
  128.                 #print "minus"
  129.         add = not add
  130.         #print "this is an iteration"
  131.         x = x + 1
  132. print(Decimal(pi))
  133. calc_time = time.time() - start_time
  134. f = open("pi.txt","r")
  135. result = ""
  136. correct = 0
  137. for digit in str(Decimal(pi)):
  138.         if digit == f.read(1):
  139.                 result = result + "T"
  140.                 if digit != ".":
  141.                         correct = correct + 1
  142.         else:
  143.                 result = result + "F"
  144. print(result)
  145. f.close()
  146. #check_time = time.time() - start_time - calc_time
  147. print "It took " + str(calc_time) + " seconds to calculate pi with " + str(correct) + "/" + str(places) + " digits correct."