栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

数列和 python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数列和 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... 1 +1/2+ 1/4 +1/8 +1/16 ... 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

KaTeX parse error: Expected 'EOF', got '·' at position 27: …+ 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 +x4…xn-1) $

For infinity series we have :
11−r=1+r+r2+r3... frac{1}{1-r}= 1+r+r^2 +r^3 ...1−r1​=1+r+r2+r3...

So the General Formula would be:
∑rk=r1−r sum r^k = frac{r}{1-r} ∑rk=1−rr​

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 

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/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/221022.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号