Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # "The Euler Series" on p.31 in "The Pleasures of Pi,e" is
- # 1/1**2 + 1/2**2 + 2/3**2 + ... + 1/n**2 + ... --> pi**/6
- # The partial sums converge very slowly, 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
- # There's an image of the last inequality in his proof at http://www.rcblue.com/images/PinchingForEuler.jpg
- # I use this inequality after multiplying the 3 expressions by (math.pi**2/(4**(n+1)))
- # Thus upper_bound_partial_sum = (2*(4**n - 1)/3) * (math.pi**2/(4**(n+1)))
- # and lower_bound_partial_sum = (2*(4**n - 1)/3 - (2**n - 1)) * (math.pi**2/(4**(n+1)))
- # The middle expression becomes lines 68-76 below,
- # which compute the euler_series_partial_sum for a given n
- # Thus this script demos the "pinching down" (for increasing values of n from 1 to 25)
- # on the middle expression's value by the upper_bound_partial_sum from above,
- # and the lower_bound_partial_sum from below.
- import math
- 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
- >>> seq1 = "qwerty"
- >>> seq2 = "qwert"
- >>> compareSequences(seq1, seq2)
- 5
- >>> seq1 = "qwerty"
- >>> seq2 = "qwerty"
- >>> compareSequences(seq1, seq2)
- None
- """
- if len(seq2) > len(seq1):
- for index in range(len(seq2)):
- if seq2[index] == seq1[-1]:
- return index+1
- elif seq1[index] != seq2[index]:
- return index
- return None
- elif len(seq1) > len(seq2):
- for index in range(len(seq1)):
- if seq1[index] == seq2[-1]:
- return index+1
- elif seq1[index] != seq2[index]:
- return index
- return None
- else:
- for index in range(len(seq1)):
- if seq1[index] != seq2[index]:
- return index
- def demo(n):
- print("n =", n)
- print()
- upper_bound_partial_sum = (2*(4**n - 1)/3) * (math.pi**2/(4**(n+1)))
- lower_bound_partial_sum = (2*(4**n - 1)/3 - (2**n - 1)) * (math.pi**2/(4**(n+1)))
- k = 1
- summ = 0
- term_num = 1
- term = 0
- while k <= 2**n-1:
- term = 1/k**2
- summ += term
- k += 1
- euler_series_partial_sum = summ
- idx1 = compareSequences(str(upper_bound_partial_sum), str(euler_series_partial_sum))
- idx2 = compareSequences(str(lower_bound_partial_sum), str(euler_series_partial_sum))
- print(upper_bound_partial_sum, "upper_bound_partial_sum")
- if n > 1:
- print((idx1-1)*' ', '|')
- print(euler_series_partial_sum, "euler_series_partial_sum")
- if n > 1:
- print((idx2-1)*' ', '|')
- print(lower_bound_partial_sum, "lower_bound_partial_sum")
- print(45*'=')
- if __name__ == '__main__':
- for n in range(1, 26):
- demo(n)
- """
- OUTPUT
- =============================================
- n = 1
- 1.2337005501361698274 upper_bound_partial_sum
- 1.0 euler_series_partial_sum
- 0.61685027506808491368 lower_bound_partial_sum
- =============================================
- n = 2
- 1.5421256876702122842 upper_bound_partial_sum
- |
- 1.3611111111111111111 euler_series_partial_sum
- |
- 1.0794879813691485989 lower_bound_partial_sum
- =============================================
- n = 3
- 1.6192319720537228984 upper_bound_partial_sum
- |
- 1.5117970521541950113 euler_series_partial_sum
- |
- 1.3493599767114357487 lower_bound_partial_sum
- =============================================
- n = 4
- 1.638508543149600552 upper_bound_partial_sum
- |
- 1.5804402834449870747 euler_series_partial_sum
- |
- 1.4939342599305181503 lower_bound_partial_sum
- =============================================
- n = 5
- 1.6433276859235699653 upper_bound_partial_sum
- |
- 1.6131907003279243463 euler_series_partial_sum
- |
- 1.5686309729270440578 lower_bound_partial_sum
- =============================================
- n = 6
- 1.6445324716170623187 upper_bound_partial_sum
- |
- 1.6291863607838875063 euler_series_partial_sum
- |
- 1.6065817222720531883 lower_bound_partial_sum
- =============================================
- n = 7
- 1.644833668040435407 upper_bound_partial_sum
- |
- 1.6370909697982118213 euler_series_partial_sum
- |
- 1.6257076951562442976 lower_bound_partial_sum
- =============================================
- n = 8
- 1.6449089671462786791 upper_bound_partial_sum
- |
- 1.6410201775196180401 euler_series_partial_sum
- |
- 1.6353083311512614884 lower_bound_partial_sum
- =============================================
- n = 9
- 1.6449277919227394971 upper_bound_partial_sum
- |
- 1.6429790332578311385 euler_series_partial_sum
- |
- 1.6401180615370004928 lower_bound_partial_sum
- =============================================
- n = 10
- 1.6449324981168547016 upper_bound_partial_sum
- |
- 1.6439570273558478339 euler_series_partial_sum
- |
- 1.6425252798269275972 lower_bound_partial_sum
- =============================================
- n = 11
- 1.6449336746653835028 upper_bound_partial_sum
- |
- 1.644445666369534333 euler_series_partial_sum
- |
- 1.64372947724615555 lower_bound_partial_sum
- =============================================
- n = 12
- 1.644933968802515703 upper_bound_partial_sum
- |
- 1.6446898964184787296 euler_series_partial_sum
- |
- 1.6443317230243356265 lower_bound_partial_sum
- =============================================
- n = 13
- 1.6449340423367987531 upper_bound_partial_sum
- |
- 1.6448119890848426746 euler_series_partial_sum
- |
- 1.6446328826805671898 lower_bound_partial_sum
- =============================================
- n = 14
- 1.6449340607203695156 upper_bound_partial_sum
- |
- 1.6448730298292933916 euler_series_partial_sum
- |
- 1.6447834717004683527 lower_bound_partial_sum
- =============================================
- n = 15
- 1.6449340653162622063 upper_bound_partial_sum
- |
- 1.6449035488044354121 euler_series_partial_sum
- |
- 1.6448587685083652795 lower_bound_partial_sum
- =============================================
- n = 16
- 1.6449340664652353789 upper_bound_partial_sum
- |
- 1.6449188079427480223 euler_series_partial_sum
- |
- 1.6448964174868003292 lower_bound_partial_sum
- =============================================
- n = 17
- 1.6449340667524786721 upper_bound_partial_sum
- |
- 1.6449264374245912819 euler_series_partial_sum
- |
- 1.6449152421196395006 lower_bound_partial_sum
- =============================================
- n = 18
- 1.6449340668242894954 upper_bound_partial_sum
- |
- 1.6449302521436848444 euler_series_partial_sum
- |
- 1.644924654471964498 lower_bound_partial_sum
- =============================================
- n = 19
- 1.6449340668422422012 upper_bound_partial_sum
- |
- 1.6449321594977746331 euler_series_partial_sum
- |
- 1.6449293606571033496 lower_bound_partial_sum
- =============================================
- n = 20
- 1.6449340668467303777 upper_bound_partial_sum
- |
- 1.6449331131734552825 euler_series_partial_sum
- |
- 1.6449317137519168636 lower_bound_partial_sum
- =============================================
- n = 21
- 1.6449340668478524218 upper_bound_partial_sum
- |
- 1.6449335900109545462 euler_series_partial_sum
- |
- 1.6449328902998846427 lower_bound_partial_sum
- =============================================
- n = 22
- 1.6449340668481329328 upper_bound_partial_sum
- |
- 1.644933828429618913 euler_series_partial_sum
- |
- 1.6449334785740087877 lower_bound_partial_sum
- =============================================
- n = 23
- 1.6449340668482030606 upper_bound_partial_sum
- |
- 1.6449339476389297795 euler_series_partial_sum
- |
- 1.6449337727111059241 lower_bound_partial_sum
- =============================================
- n = 24
- 1.6449340668482205925 upper_bound_partial_sum
- |
- 1.6449340072435798859 euler_series_partial_sum
- |
- 1.6449339197796632583 lower_bound_partial_sum
- =============================================
- n = 25
- 1.6449340668482249755 upper_bound_partial_sum
- |
- 1.644934037045903606 euler_series_partial_sum
- |
- 1.6449339933139441169 lower_bound_partial_sum
- =============================================
- """
Advertisement
Add Comment
Please, Sign In to add comment