Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # "The Euler Series" p.31 in "The Pleasures of Pi,e"
- # 1/1**2 + 1/2**2 + 2/3**2 + ... + 1/n**2 + ... = pi**/6 (limit) (very slow to converge)
- # So I use Hans Lundmark's post on http://math.stackexchange.com, at
- # http://math.stackexchange.com/questions/8337/different-methods-to-compute-sum-n-1-infty-frac1n2
- from mpmath import *; mp.dps = 100; mp.pretty = True
- def compareSequences(seq1, seq2):
- """
- Returns first index at which two sequences differ
- >>> seq1 = "Now is the time"
- >>> seq2 = "Now was the time"
- >>> compareSequences(seq1, seq2)
- 4
- """
- for index in range(len(seq2)):
- if seq1[index] != seq2[index]:
- return index
- return None
- def int_commas(n):
- """
- inserts commas into integers and returns the string
- E.g. -12345678 -> -12,345,789
- """
- return format(n, ',d')
- n = mpf(150)
- print("n =", int_commas(int(n)))
- print()
- limit = (pi()**mpf(2))/mpf(6)
- upper_bound_partial_sum = (mpf(2)*(4**n - 1)/mpf(3)) * (pi()**mpf(2)/(4**(n+1)))
- idx1 = compareSequences(str(upper_bound_partial_sum), str(limit))
- lower_bound_partial_sum = (mpf(2)*(4**n - 1)/mpf(3) - (mpf(2)**n - 1)) * (pi()**mpf(2)/(4**(n+1)))
- idx2 = compareSequences(str(lower_bound_partial_sum), str(limit))
- print(limit, "limit (PI**2/6)")
- print((idx1-1)*' ', '^')
- print(upper_bound_partial_sum, "upper_bound_partial_sum")
- print()
- print()
- print(limit, "limit (PI**2/6)")
- print((idx2-1)*' ', '^')
- print(lower_bound_partial_sum, "lower_bound_partial_sum")
- """
- OUTPUT
- n = 150
- 1.644934066848226436472415166646025189218949901206798437735558229370007470403200873833628900619758705 limit (PI**2/6)
- ^
- 1.644934066848226436472415166646025189218949901206798437735558229370007470403200873833628899812245197 upper_bound_partial_sum
- 1.644934066848226436472415166646025189218949901206798437735558229370007470403200873833628900619758705 limit (PI**2/6)
- ^
- 1.644934066848226436472415166646025189218949899478015751393061460363041277434208815632129055954370542 lower_bound_partial_sum
- """
Advertisement
Add Comment
Please, Sign In to add comment