{ "cells": [ { "cell_type": "markdown", "id": "6e325ea0", "metadata": {}, "source": [ "[Home](Home.ipynb)\n", "\n", "# Worksheet: Generating $\\pi$\n", "\n", "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.\n", "\n", "Although I'm putting my name to it as an author (kurner), I acquired this gem [from edu-sig](https://mail.python.org/archives/list/edu-sig@python.org/thread/SOXS2AQRIH252LEB4SXUPQ3DJAXRLY67/), the discussion list for those interested in Python's role in education within the Python.org domain.\n", "\n", "The generator below keeps delivering the next decimal digit of $\\pi$ starting with 3141... and so on." ] }, { "cell_type": "code", "execution_count": 3, "id": "66c572eb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952\n" ] } ], "source": [ "# %load gen_pi.py\n", "\"\"\"\n", "Created on Thu Dec 3 16:08:49 2015\n", "\n", "@author: kurner\n", "\n", "Another generator example: converging to Pi\n", "\n", "https://mail.python.org/pipermail/edu-sig/2015-September/date.html\n", "\n", "\"\"\"\n", "\n", "def pi():\n", "\n", " k, a, b, a1, b1 = 2, 4, 1, 12, 4\n", " while True:\n", " p, q, k = k*k, 2*k+1, k+1\n", " a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1\n", " d, d1 = a/b, a1/b1\n", " while d == d1:\n", " yield int(d)\n", " a, a1 = 10*(a%b), 10*(a1%b1)\n", " d, d1 = a/b, a1/b1\n", "#%%\n", "if __name__ == \"__main__\": \n", " the_gen = pi()\n", " num = 0\n", " for digit in the_gen:\n", " print(digit,end=\"\")\n", " if num > 1005:\n", " break\n", " num = num + 1\n", " \n", " print()\n" ] }, { "cell_type": "markdown", "id": "e045c9be", "metadata": {}, "source": [ "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__```." ] }, { "cell_type": "code", "execution_count": 4, "id": "bb80f9f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893\n" ] } ], "source": [ "! python gen_pi.py" ] }, { "cell_type": "code", "execution_count": null, "id": "722f9600", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 5 }