數列和和python

Background Story :

很多數學家一起去了一個酒吧,第一個要了一杯啤酒,第二個要了 1/2 杯啤酒, 第三個要了 1/4 杯啤酒, 第四個要了 1/8啤酒。 那個漂亮的bartender 翻了個白眼,給了他們兩杯啤酒,說, 你們自己去分吧! 😍

Question: What is the Summation of

1 +1/2+ 1/4 +1/8 +1/16 ...

[caption id="attachment_1733" align="alignnone" width="750"]

RitaE / Pixabay[/caption]

Solution:

This is a famous example of a Geometric series

1/2 + 1/4 + 1/8 + 1/16 + · · ·

The fundamental idea is the base on the formula:

a^2 - b^2= (a-b)(a+b)

1-x^n= (1-x)(1+x+x^2 +x^3 +x^4....x^n-1)

For infinity series we have :
\frac{1}{1-r}= 1+r+r^2 +r^3 ...

So the General Formula would be:
\sum r^k = \frac{r}{1-r}

Then we have the result to be 1+ 1 =2 Beers :

Now we can check if Python does a good job with computing the Series.

def SumofGeo(a,r,n):
    sum=0
    i=0
    while i <n:
        sum=sum+a
        a=a*r
        i=i+1
    return sum

SumofGeo(1,1/2,5)
SumofGeo(1,1/2,10)
SumofGeo(1,1/2,100)

Python Output:

Out[6]: 1.9375

Out[7]: 1.998046875

Out[8]: 2.0

As we can see when n=5, the geometric sum of (1/2)^n is 1.93, when n=10, the sum is 1.99. when n=100, python assumed it is 2. Remember theoretically, it is not 2 yet, the sum is 2 when n approaches infinity!

So the Hot BarTender is smart!!

Cheers and Happy Studying! 🙇‍♀️

References:

https://en.wikipedia.org/wiki/Geometric_series
https://www.geeksforgeeks.org/program-sum-geometric-series/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章