We came back to the topic of generators on the last day of class (August 20, 2021). The one below is quite mysterious to me in that I can't really fathom how it works. But it works.
Although I'm putting my name to it as an author (kurner), I acquired this gem from edu-sig, the discussion list for those interested in Python's role in education within the Python.org domain.
The generator below keeps delivering the next decimal digit of $\pi$ starting with 3141... and so on.
# %load gen_pi.py
"""
Created on Thu Dec 3 16:08:49 2015
@author: kurner
Another generator example: converging to Pi
https://mail.python.org/pipermail/edu-sig/2015-September/date.html
"""
def pi():
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while True:
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a/b, a1/b1
while d == d1:
yield int(d)
a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a/b, a1/b1
#%%
if __name__ == "__main__":
the_gen = pi()
num = 0
for digit in the_gen:
print(digit,end="")
if num > 1005:
break
num = num + 1
print()
31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952
As we see from the above code, the module is triggered to run out pi ($\pi$) out to a thousand places if run top level as __main__
. Any module running top level under Python, i.e. not imported by another module, gets the name __main__
.
! python gen_pi.py
314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893