pyzuki

demo of a proof of The Euler Series

Aug 12th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.30 KB | None | 0 0
  1. # "The Euler Series" on p.31 in "The Pleasures of Pi,e" is
  2.  
  3. # 1/1**2 + 1/2**2 + 2/3**2 + ... + 1/n**2 + ... --> pi**/6  
  4.  
  5. # The partial sums converge very slowly, so I use Hans Lundmark's post on http://math.stackexchange.com, at
  6. # http://math.stackexchange.com/questions/8337/different-methods-to-compute-sum-n-1-infty-frac1n2
  7.  
  8. # There's an image of the last inequality in his proof at http://www.rcblue.com/images/PinchingForEuler.jpg
  9.  
  10. # I use this inequality after multiplying the 3 expressions by (math.pi**2/(4**(n+1)))
  11. # Thus upper_bound_partial_sum = (2*(4**n - 1)/3) * (math.pi**2/(4**(n+1)))
  12. # and lower_bound_partial_sum = (2*(4**n - 1)/3 - (2**n - 1)) * (math.pi**2/(4**(n+1)))
  13. # The middle expression becomes lines 68-76 below,
  14. # which compute the euler_series_partial_sum for a given n
  15.  
  16. # Thus this script demos the "pinching down" (for increasing values of n from 1 to 25)
  17. # on the middle expression's value by the upper_bound_partial_sum from above,
  18. # and the lower_bound_partial_sum from below.
  19.    
  20.  
  21. import math
  22.  
  23. def compareSequences(seq1, seq2):
  24.     """
  25.    Returns first index at which two sequences differ
  26.  
  27.    >>> seq1 = "Now is the time"
  28.    >>> seq2 = "Now was the time"
  29.    >>> compareSequences(seq1, seq2)
  30.    4
  31.    >>> seq1 = "qwerty"
  32.    >>> seq2 = "qwert"
  33.    >>> compareSequences(seq1, seq2)
  34.    5
  35.    >>> seq1 = "qwerty"
  36.    >>> seq2 = "qwerty"
  37.    >>> compareSequences(seq1, seq2)
  38.    None
  39.    """
  40.     if len(seq2) > len(seq1):
  41.         for index in range(len(seq2)):
  42.             if seq2[index] == seq1[-1]:
  43.                 return index+1
  44.             elif seq1[index] != seq2[index]:
  45.                 return index
  46.         return None
  47.    
  48.     elif len(seq1) > len(seq2):
  49.         for index in range(len(seq1)):
  50.             if seq1[index] == seq2[-1]:
  51.                 return index+1
  52.             elif seq1[index] != seq2[index]:
  53.                 return index
  54.         return None
  55.    
  56.     else:
  57.         for index in range(len(seq1)):
  58.             if seq1[index] != seq2[index]:
  59.                 return index
  60.  
  61. def demo(n):
  62.     print("n =", n)
  63.     print()
  64.    
  65.     upper_bound_partial_sum = (2*(4**n - 1)/3) * (math.pi**2/(4**(n+1)))
  66.     lower_bound_partial_sum = (2*(4**n - 1)/3 - (2**n - 1)) * (math.pi**2/(4**(n+1)))
  67.    
  68.     k = 1
  69.     summ = 0
  70.     term_num = 1
  71.     term = 0
  72.     while k <= 2**n-1:
  73.         term = 1/k**2
  74.         summ += term
  75.         k += 1
  76.     euler_series_partial_sum = summ
  77.    
  78.     idx1 = compareSequences(str(upper_bound_partial_sum), str(euler_series_partial_sum))
  79.     idx2 = compareSequences(str(lower_bound_partial_sum), str(euler_series_partial_sum))
  80.    
  81.     print(upper_bound_partial_sum, "upper_bound_partial_sum")
  82.     if n > 1:
  83.         print((idx1-1)*' ', '|')
  84.     print(euler_series_partial_sum, "euler_series_partial_sum")
  85.     if n > 1:
  86.         print((idx2-1)*' ', '|')
  87.     print(lower_bound_partial_sum, "lower_bound_partial_sum")
  88.     print(45*'=')
  89.  
  90. if __name__ == '__main__':
  91.     for n in range(1, 26):
  92.         demo(n)
  93.  
  94. """
  95. OUTPUT
  96. =============================================
  97. n = 1
  98.  
  99. 1.2337005501361698274 upper_bound_partial_sum
  100. 1.0 euler_series_partial_sum
  101. 0.61685027506808491368 lower_bound_partial_sum
  102. =============================================
  103. n = 2
  104.  
  105. 1.5421256876702122842 upper_bound_partial_sum
  106.  |
  107. 1.3611111111111111111 euler_series_partial_sum
  108.  |
  109. 1.0794879813691485989 lower_bound_partial_sum
  110. =============================================
  111. n = 3
  112.  
  113. 1.6192319720537228984 upper_bound_partial_sum
  114.  |
  115. 1.5117970521541950113 euler_series_partial_sum
  116.  |
  117. 1.3493599767114357487 lower_bound_partial_sum
  118. =============================================
  119. n = 4
  120.  
  121. 1.638508543149600552 upper_bound_partial_sum
  122.  |
  123. 1.5804402834449870747 euler_series_partial_sum
  124.  |
  125. 1.4939342599305181503 lower_bound_partial_sum
  126. =============================================
  127. n = 5
  128.  
  129. 1.6433276859235699653 upper_bound_partial_sum
  130.   |
  131. 1.6131907003279243463 euler_series_partial_sum
  132.  |
  133. 1.5686309729270440578 lower_bound_partial_sum
  134. =============================================
  135. n = 6
  136.  
  137. 1.6445324716170623187 upper_bound_partial_sum
  138.   |
  139. 1.6291863607838875063 euler_series_partial_sum
  140.   |
  141. 1.6065817222720531883 lower_bound_partial_sum
  142. =============================================
  143. n = 7
  144.  
  145. 1.644833668040435407 upper_bound_partial_sum
  146.   |
  147. 1.6370909697982118213 euler_series_partial_sum
  148.   |
  149. 1.6257076951562442976 lower_bound_partial_sum
  150. =============================================
  151. n = 8
  152.  
  153. 1.6449089671462786791 upper_bound_partial_sum
  154.    |
  155. 1.6410201775196180401 euler_series_partial_sum
  156.   |
  157. 1.6353083311512614884 lower_bound_partial_sum
  158. =============================================
  159. n = 9
  160.  
  161. 1.6449277919227394971 upper_bound_partial_sum
  162.    |
  163. 1.6429790332578311385 euler_series_partial_sum
  164.    |
  165. 1.6401180615370004928 lower_bound_partial_sum
  166. =============================================
  167. n = 10
  168.  
  169. 1.6449324981168547016 upper_bound_partial_sum
  170.    |
  171. 1.6439570273558478339 euler_series_partial_sum
  172.    |
  173. 1.6425252798269275972 lower_bound_partial_sum
  174. =============================================
  175. n = 11
  176.  
  177. 1.6449336746653835028 upper_bound_partial_sum
  178.     |
  179. 1.644445666369534333 euler_series_partial_sum
  180.    |
  181. 1.64372947724615555 lower_bound_partial_sum
  182. =============================================
  183. n = 12
  184.  
  185. 1.644933968802515703 upper_bound_partial_sum
  186.     |
  187. 1.6446898964184787296 euler_series_partial_sum
  188.     |
  189. 1.6443317230243356265 lower_bound_partial_sum
  190. =============================================
  191. n = 13
  192.  
  193. 1.6449340423367987531 upper_bound_partial_sum
  194.     |
  195. 1.6448119890848426746 euler_series_partial_sum
  196.     |
  197. 1.6446328826805671898 lower_bound_partial_sum
  198. =============================================
  199. n = 14
  200.  
  201. 1.6449340607203695156 upper_bound_partial_sum
  202.     |
  203. 1.6448730298292933916 euler_series_partial_sum
  204.     |
  205. 1.6447834717004683527 lower_bound_partial_sum
  206. =============================================
  207. n = 15
  208.  
  209. 1.6449340653162622063 upper_bound_partial_sum
  210.      |
  211. 1.6449035488044354121 euler_series_partial_sum
  212.     |
  213. 1.6448587685083652795 lower_bound_partial_sum
  214. =============================================
  215. n = 16
  216.  
  217. 1.6449340664652353789 upper_bound_partial_sum
  218.      |
  219. 1.6449188079427480223 euler_series_partial_sum
  220.     |
  221. 1.6448964174868003292 lower_bound_partial_sum
  222. =============================================
  223. n = 17
  224.  
  225. 1.6449340667524786721 upper_bound_partial_sum
  226.      |
  227. 1.6449264374245912819 euler_series_partial_sum
  228.      |
  229. 1.6449152421196395006 lower_bound_partial_sum
  230. =============================================
  231. n = 18
  232.  
  233. 1.6449340668242894954 upper_bound_partial_sum
  234.       |
  235. 1.6449302521436848444 euler_series_partial_sum
  236.      |
  237. 1.644924654471964498 lower_bound_partial_sum
  238. =============================================
  239. n = 19
  240.  
  241. 1.6449340668422422012 upper_bound_partial_sum
  242.       |
  243. 1.6449321594977746331 euler_series_partial_sum
  244.      |
  245. 1.6449293606571033496 lower_bound_partial_sum
  246. =============================================
  247. n = 20
  248.  
  249. 1.6449340668467303777 upper_bound_partial_sum
  250.       |
  251. 1.6449331131734552825 euler_series_partial_sum
  252.       |
  253. 1.6449317137519168636 lower_bound_partial_sum
  254. =============================================
  255. n = 21
  256.  
  257. 1.6449340668478524218 upper_bound_partial_sum
  258.       |
  259. 1.6449335900109545462 euler_series_partial_sum
  260.       |
  261. 1.6449328902998846427 lower_bound_partial_sum
  262. =============================================
  263. n = 22
  264.  
  265. 1.6449340668481329328 upper_bound_partial_sum
  266.       |
  267. 1.644933828429618913 euler_series_partial_sum
  268.        |
  269. 1.6449334785740087877 lower_bound_partial_sum
  270. =============================================
  271. n = 23
  272.  
  273. 1.6449340668482030606 upper_bound_partial_sum
  274.       |
  275. 1.6449339476389297795 euler_series_partial_sum
  276.        |
  277. 1.6449337727111059241 lower_bound_partial_sum
  278. =============================================
  279. n = 24
  280.  
  281. 1.6449340668482205925 upper_bound_partial_sum
  282.         |
  283. 1.6449340072435798859 euler_series_partial_sum
  284.       |
  285. 1.6449339197796632583 lower_bound_partial_sum
  286. =============================================
  287. n = 25
  288.  
  289. 1.6449340668482249755 upper_bound_partial_sum
  290.         |
  291. 1.644934037045903606 euler_series_partial_sum
  292.       |
  293. 1.6449339933139441169 lower_bound_partial_sum
  294. =============================================
  295. """
Advertisement
Add Comment
Please, Sign In to add comment