{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Approximation to famous mathematical constants\n", "\n", "### Approximating $e$\n", "We know that\n", "$$e^x=\\sum_{n=0}^\\infty =1+x+\\frac{1}{2!}x^2+\\frac{1}{3!}x^3+\\cdots$$\n", "This provides us with a quickly converging series for $e$:\n", "$$e^1=e=1+\\frac{1}{2}+\\frac{1}{3!}+\\frac{1}{4!}+\\cdots$$\n", "This is the series that computers and calculators use to compute $e$ and we can\n", "use it ourselves as well. See below for a little computer program that computes\n", "the partial sums for the series above." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0. 1.0000000000000000000000000000000000000000\n", " 1. 2.0000000000000000000000000000000000000000\n", " 2. 2.5000000000000000000000000000000000000000\n", " 3. 2.6666666666666665186369300499791279435158\n", " 4. 2.7083333333333330372738600999582558870316\n", " 5. 2.7166666666666663410012461099540814757347\n", " 6. 2.7180555555555554470004153699846938252449\n", " 7. 2.7182539682539683667528152000159025192261\n", " 8. 2.7182787698412700372330164100276306271553\n", " 9. 2.7182815255731922476911677222233265638351\n", "10. 2.7182818011463845131459038384491577744484\n", "11. 2.7182818261984929009145162126515060663223\n", "12. 2.7182818282861687109175363730173557996750\n", "13. 2.7182818284467593628050963161513209342957\n", "14. 2.7182818284582301870955234335269778966904\n", "15. 2.7182818284589949087148852413520216941833\n", "16. 2.7182818284590428703495490481145679950714\n", "17. 2.7182818284590455348848081484902650117874\n", "18. 2.7182818284590455348848081484902650117874\n", "19. 2.7182818284590455348848081484902650117874\n", "20. 2.7182818284590455348848081484902650117874\n", "21. 2.7182818284590455348848081484902650117874\n", "22. 2.7182818284590455348848081484902650117874\n", "23. 2.7182818284590455348848081484902650117874\n", "24. 2.7182818284590455348848081484902650117874\n", "25. 2.7182818284590455348848081484902650117874\n", "26. 2.7182818284590455348848081484902650117874\n", "27. 2.7182818284590455348848081484902650117874\n", "28. 2.7182818284590455348848081484902650117874\n", "29. 2.7182818284590455348848081484902650117874\n", "30. 2.7182818284590455348848081484902650117874\n", "31. 2.7182818284590455348848081484902650117874\n", "32. 2.7182818284590455348848081484902650117874\n", "33. 2.7182818284590455348848081484902650117874\n", "34. 2.7182818284590455348848081484902650117874\n", "35. 2.7182818284590455348848081484902650117874\n", "36. 2.7182818284590455348848081484902650117874\n", "37. 2.7182818284590455348848081484902650117874\n", "38. 2.7182818284590455348848081484902650117874\n", "39. 2.7182818284590455348848081484902650117874\n" ] } ], "source": [ "import math\n", "\n", "N=40\n", "S=0\n", "for n in range(N):\n", " S=S+1/math.factorial(n)\n", " print('{:2d}. {:.40f}'.format(n,S))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Approximating $\\pi$\n", "\n", "We know that $\\tan(\\pi/4)=1$, so\n", "$$4\\arctan(1)=\\pi$$\n", "Furthermore, we know that for $-1\\lt x\\leq 1$ we have\n", "$$\\arctan(x)=\\sum_{n=0}^\\infty\n", "\\frac{(-1)^n}{2n+1}x^{2n+1}=1-\\frac{x^3}{3}+\\frac{x^5}{5}-\\frac{x^7}{7}+\\cdots$$\n", "so we find that\n", "$$\\pi=4-\\frac{4}{3}+\\frac{4}{5}-\\frac{4}{7}+\\frac{4}{9}+\\cdots$$\n", "which is a series that behaves comparable to an alternating harmonic series,\n", "so it converges (and in fact the partial sums give upper and lower limits on the\n", "value), but it converges relatively slowly." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0. 4.0000000000000000000000000000000000000000\n", " 10. 3.2323158094055939315580872062128037214279\n", " 20. 3.1891847822775960530350403132615610957146\n", " 30. 3.1738423371907504844102732022292912006378\n", " 40. 3.1659792728432156749818204843904823064804\n", " 50. 3.1611986129870506267991459026234224438667\n", " 60. 3.1579849951686660425309582933550700545311\n", " 70. 3.1556764623074751163755991001380607485771\n", " 80. 3.1539378622726155221300814446294680237770\n", " 90. 3.1525813328751204345223868585890159010887\n", "100. 3.1514934010709914069536807801341637969017\n", "110. 3.1506014798194978965284462901763617992401\n", "120. 3.1498569752932739085338198492536321282387\n", "130. 3.1492261301786892424559027858776971697807\n", "140. 3.1486847629938385928483057796256616711617\n", "150. 3.1482150975379377122465029970044270157814\n", "160. 3.1478037738120017152709806396160274744034\n", "170. 3.1474405568104151242891930451150983572006\n", "180. 3.1471174733094979814040925703011453151703\n", "190. 3.1468282198062982324415770563064143061638\n", "200. 3.1465677471829556210991540865506976842880\n", "210. 3.1463319634705033678301333566196262836456\n", "220. 3.1461175173068993693448192061623558402061\n", "230. 3.1459216376375844781421164952917024493217\n", "240. 3.1457420133232325554217823082581162452698\n", "250. 3.1455767015255999830003474926343187689781\n", "260. 3.1454240571534981540935405064374208450317\n", "270. 3.1452826779291465086885182245168834924698\n", "280. 3.1451513611838510797724666190333664417267\n", "290. 3.1450290695615721325850699940929189324379\n", "300. 3.1449149035588526146511867409572005271912\n", "310. 3.1448080793623969242389648570679128170013\n", "320. 3.1447079108290805216086027940036728978157\n", "330. 3.1446137947323693495604857162106782197952\n", "340. 3.1445251986046298675603338779183104634285\n", "350. 3.1444416506576473580025776755064725875854\n", "360. 3.1443627313783775178990254062227904796600\n", "370. 3.1442880664838579463093992671929299831390\n", "380. 3.1442173209855672766366296855267137289047\n", "390. 3.1441501941646152751275167247513309121132\n", "400. 3.1440864152987608726164125982904806733131\n", "410. 3.1440257400132236575984734372468665242195\n", "420. 3.1439679471515558084604435862274840474129\n", "430. 3.1439128360821246843670451198704540729523\n", "440. 3.1438602243710556116695897799218073487282\n", "450. 3.1438099457647581402852665632963180541992\n", "460. 3.1437618484350284830952659831382334232330\n", "470. 3.1437157934476989140648583997972309589386\n", "480. 3.1436716534223174690509949869010597467422\n", "490. 3.1436293113555984213292049389565363526344\n", "500. 3.1435886595857889247440652980003505945206\n", "510. 3.1435495988786286147842474747449159622192\n", "520. 3.1435120376185743396035832120105624198914\n", "530. 3.1434758910914082363774468831252306699753\n", "540. 3.1434410808464154918340227595763280987740\n", "550. 3.1434075341280065529758758202660828828812\n", "560. 3.1433751833681351506299961329204961657524\n", "570. 3.1433439657320509930116259056376293301582\n", "580. 3.1433138227109576057216600020183250308037\n", "590. 3.1432846997560317525710615882417187094688\n", "600. 3.1432565459489740788967537810094654560089\n", "610. 3.1432293137049125419935080572031438350677\n", "620. 3.1432029585040153207842195115517824888229\n", "630. 3.1431774386486148742392288113478571176529\n", "640. 3.1431527150430764727673249581130221486092\n", "650. 3.1431287509939465074637610086938366293907\n", "660. 3.1431055120282462844727433548541739583015\n", "670. 3.1430829657280119349138658435549587011337\n", "680. 3.1430610815794142176571313029853627085686\n", "690. 3.1430398308349767333425006654579192399979\n", "700. 3.1430191863875864832778006530134007334709\n", "710. 3.1429991226551430294477995630586519837379\n", "720. 3.1429796154747946523855262057622894644737\n", "730. 3.1429606420058719962185023177880793809891\n", "740. 3.1429421806406652173393467819551005959511\n", "750. 3.1429242109223372025894605030771344900131\n", "760. 3.1429067134693116081223251967458054423332\n", "770. 3.1428896699055379748699579067761078476906\n", "780. 3.1428730627961183330398853286169469356537\n", "790. 3.1428568755878178997420491214143112301826\n", "800. 3.1428410925540282150336679478641599416733\n", "810. 3.1428256987438030201076344383182004094124\n", "820. 3.1428106799346227084868132806150242686272\n", "830. 3.1427960225885631651010498899267986416817\n", "840. 3.1427817138115954342936220200499519705772\n", "850. 3.1427677413157555363909523293841630220413\n", "860. 3.1427540933839468451083121181000024080276\n", "870. 3.1427407588371707447549852076917886734009\n", "880. 3.1427277270039861711836692848009988665581\n", "890. 3.1427149876920301707627913856413215398788\n", "900. 3.1427025311614293912043649470433592796326\n", "910. 3.1426903480999661688599644548958167433739\n", "920. 3.1426784295998619889189740206347778439522\n", "930. 3.1426667671360566380656109686242416501045\n", "940. 3.1426553525458706950246323685860261321068\n", "950. 3.1426441780099563239048165996791794896126\n", "960. 3.1426332360344328975543248816393315792084\n", "970. 3.1426225194341244062457008112687617540359\n", "980. 3.1426120213168311501306106947595253586769\n", "990. 3.1426017350685424567302561627002432942390\n" ] } ], "source": [ "N=1000\n", "S=0\n", "\n", "for n in range(N):\n", " S = S + 4*(-1)**n/(2*n+1)\n", " if (n%10)==0: print('{:3d}. {:.40f}'.format(n,S))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do better. With a bit of trigonometry (in particular, the angle summation formulas for $\\sin$ and $\\cos$) one can prove\n", "$$\\arctan\\frac{1}{2}+\\arctan\\frac{1}{3}=\\arctan\\frac{3+2}{2\\cdot3-1}=\\frac{\\pi}{4}$$\n", "so we get\n", "$$\\pi=4\\sum_{n=0}^\\infty \\frac{(-1)^n}{(2n+1)}\\left(\\frac{1}{2}\\right)^{2n+1}+4\\sum_{n=0}^\\infty\n", "\\frac{(-1)^n}{(2n+1) }\\left(\\frac{1}{3}\\right)^{2n+1}=\n", "\\sum_{n=0}^\\infty \\frac{4(-1)^n}{2n+1} \\left(\\left(\\frac{1}{2}\\right)^{2n+1}+\\left(\\frac{1}{3}\\right)^{2n+1}\\right) \n", "$$\n", "which converges MUCH faster." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0. 3.3333333333333330372738600999582558870316\n", " 1. 3.1172839506172835832842338277259841561317\n", " 2. 3.1455761316872425936708168592303991317749\n", " 3. 3.1408505617610553706242626503808423876762\n", " 4. 3.1417411974336886260061874054372310638428\n", " 5. 3.1415615878775904334929691685829311609268\n", " 6. 3.1415993409661977509017560805659741163254\n", " 7. 3.1415911843609061193660636490676552057266\n", " 8. 3.1415929813345662147128223296022042632103\n", " 9. 3.1415925796063506325594971713144332170486\n", "10. 3.1415926704506853539555777388159185647964\n", "11. 3.1415926497167876441096723283408209681511\n", "12. 3.1415926544853478574736982409376651048660\n", "13. 3.1415926533815388133064061548793688416481\n", "14. 3.1415926536384572997917530301492661237717\n", "15. 3.1415926535783715856098297081189230084419\n", "16. 3.1415926535924825202528154477477073669434\n", "17. 3.1415926535891562920710384787525981664658\n", "18. 3.1415926535899427740616829396458342671394\n", "19. 3.1415926535897562565935459133470430970192\n", "20. 3.1415926535898006655145309196086600422859\n", "21. 3.1415926535897900073734945181058719754219\n", "22. 3.1415926535897926719087536184815689921379\n", "23. 3.1415926535897922278195437684189528226852\n", "24. 3.1415926535897922278195437684189528226852\n", "25. 3.1415926535897922278195437684189528226852\n", "26. 3.1415926535897922278195437684189528226852\n", "27. 3.1415926535897922278195437684189528226852\n", "28. 3.1415926535897922278195437684189528226852\n", "29. 3.1415926535897922278195437684189528226852\n", "30. 3.1415926535897922278195437684189528226852\n", "31. 3.1415926535897922278195437684189528226852\n", "32. 3.1415926535897922278195437684189528226852\n", "33. 3.1415926535897922278195437684189528226852\n", "34. 3.1415926535897922278195437684189528226852\n", "35. 3.1415926535897922278195437684189528226852\n", "36. 3.1415926535897922278195437684189528226852\n", "37. 3.1415926535897922278195437684189528226852\n", "38. 3.1415926535897922278195437684189528226852\n", "39. 3.1415926535897922278195437684189528226852\n", "40. 3.1415926535897922278195437684189528226852\n" ] } ], "source": [ "N=40\n", "S=0\n", "\n", "for n in range(N+1):\n", " term = (4*(-1)**n)/(2*n+1)*( (1/2)**(2*n+1)+(1/3)**(2*n+1))\n", " S=S+term\n", " print('{:2d}. {:.40f}'.format(n,S))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For comparison here are the first $40$ digits of $\\pi$." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1415926535897931159979634685441851615906\n" ] } ], "source": [ " print('{:.40f}'.format(math.pi))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }