Upgrade to Pro — share decks privately, control downloads, hide ads and more …

"Bytes of Pi" by David Fraser

Pycon ZA
October 08, 2020

"Bytes of Pi" by David Fraser

This is a talk on how to calculate Pi. It was originally done for bright high school maths students - so it is also an example of an approach to interesting such students in a programming career. It includes a brief tour of the algorithm development behind calculating Pi to many decimal places, and an example of making a functioning web page that does this. And a mug. All with an iPython notebook to illustrate things along the way.

Pycon ZA

October 08, 2020
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. 1
    Bytes of Pi
    David Fraser
    PyCon ZA, 2020-10-08

    View Slide

  2. 2
    Inspiring mathematical youth to program
    The Back Story

    View Slide

  3. 3
    Outline
    | hexagon.com
    • Hand-Calculating Pi
    • Polygons, Pythagoras, Archimedes
    • Infinite Series
    • Decimal, Binary and Bases
    • Computer Calculations
    • Spigot Algorithms
    • Coffee Mugs
    2020
    10
    = 1 1 1 1 1 1 0 0 1 0 0
    2
    = 210 +29+28+27+26+25 +22
    = 7 E 4
    16
    = 7x163 + 14x162 + 24x160

    View Slide

  4. 4
    Using a compass and a piece of string
    Estimating Pi by Hand

    View Slide

  5. 5
    Moving towards increased precision
    Approximation with Polygons

    View Slide

  6. 6
    Geometry, algebra, and persistence
    Archimedes and Polygons

    View Slide

  7. 7
    Working out increasingly accurate values
    Like Archimedes, but squares

    View Slide

  8. 8
    Like Archimedes, but squares
    Working out increasingly accurate values
    Exponent Sides Lower Bound Upper Bound
    2 4 2.828427 4.000000
    3 8 3.061467 3.313708
    4 16 3.121445 3.182598
    5 32 3.136548 3.151725
    11 2048 3.141591 3.141595

    View Slide

  9. 9
    Exact values that take forever to calculate
    Infinite Series

    View Slide

  10. 10
    Srinivasa Ramanujan
    Infinite Series
    1 3.141592730013305523328881463385
    2 3.141592653589794004176383168669
    3 3.141592653589793115997963468544
    4 3.141592653589793115997963468544
    5 3.141592653589793115997963468544

    View Slide

  11. 11
    Decimal, Binary and Computers
    Number Systems and Bases
    2020
    10
    = 1 1 1 1 1 1 0 0 1 0 0
    2
    = 210 +29+28+27+26+25 +22
    = 7 E 4
    16
    = 7x163 + 14x162 + 24x160

    View Slide

  12. 12
    Fixed bits, negative numbers, etc
    Storing Numbers in Computers
    2020
    10
    in 16 bits:
    = 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0
    2
    -2020
    10
    , using a sign bit
    = 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0
    2
    -2020
    10
    , using two’s complement: invert bits, +1
    = 1 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0
    2
    calculating 2020
    10
    +(-2020
    10
    ):
    0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0
    2
    + 1 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0
    2
    = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    2

    View Slide

  13. 13
    Floating point, etc
    Storing Numbers in Computers
    sign bit exponent mantissa
    0 10000000 10010010000111111011011
    2
    Value = (-1)sign x 2exponent-127 x (1 + 0.mantissa)
    How does pi look in a floating point number?
    sign bit exponent mantissa
    = 0 10000000 10010010000111111011011
    2
    which translates to
    = (-1)0 x 21 x (1 + 0.5707963705062866)
    = 3.1415927410125732

    View Slide

  14. 14
    Srinivasa Ramanujan
    Infinite Series
    1 3.1415927300133056603139961890252155185995816071102
    2 3.1415926535897938779989058263060130942166450293230
    3 3.1415926535897932384626490657027588981566774804625
    4 3.1415926535897932384626433832795552731599742104206
    5 3.1415926535897932384626433832795028841976638181331
    6 3.1415926535897932384626433832795028841971693993799

    View Slide

  15. 15
    Record # of digits of Pi calculated
    Era of Computers
    Year Record-holder Time Digits of Pi
    1949 G W Reitwiesner et al 70 hours 2,037
    1958 Francois Genuys 1.7 hours 10,000
    1961 Daniel Shanks & John Wrensch 8.7 hours 100,000
    1973 Hean Guilloud and M. Dichampt 28 hours 1,001,250
    1983 Yasumasa Kanada, Sayaka Yoshino and Yoshiaki Tamura 16,777,206
    1987 Yasumasa Kanada, Yoshiaki Tamura, Yoshinobu Kubo et al 134,214,700
    1989 Gregory V. Chudnovsky & David V. Chudnovsky 480,000,000
    1991 Gregory V. Chudnovsky & David V. Chudnovsky 2,260,000,000
    1997 Yasumasa Kanada and Daisuke Takahashi 51,539,600,000
    1999 Yasumasa Kanada and Daisuke Takahashi 206,158,430,000
    2002 Yasumasa Kanada & 9 person team (64 computing nodes) 600 hours 1,241,100,000,000
    2009 Fabrice Bellard (with home computer) 131 days 2,699,999,990,000
    2010 Shigeru Kondo (with home computer) 90 days 5,000,000,000,000
    2011 Shigeru Kondo (with home computer) 371 days 10,000,000,000,050
    2019 Emma Haruka Iwao (using cloud computer) 121 days 31,415,926,535,897
    2020 Timothy Mullican (using home computers) 303 days 50,000,000,000,000

    View Slide

  16. 16
    Dripping delicious decimal digits
    Spigot Algorithms

    View Slide

  17. 17
    Multi-Radix, Time and Irrationality
    Varying-Base Number Systems
    2020-10-08 16:30:00
    2020*365 + 10*30 + 8 + 16/24 + 30/(24*60) + 00/(24*60*60)
    = 2020
    365/30
    1030
    8 1624
    3060
    060

    View Slide

  18. 18
    Dripping delicious decimal digits
    Spigot Algorithms

    View Slide

  19. 19
    Some actual code
    Spigot Algorithms
    import sys
    write = sys.stdout.write
    n = 1000
    l = 10*n//3
    a = [2 for j in range(0, l)]
    nines, predigit, started = 0, 0, False
    for j in range(0, n):
    q = 0
    for i in range(l-1, -1, -1):
    x = 10*a[i] + q*(i+1)
    a[i] = x % (2*i+1)
    q = x // (2*i+1)
    q, a[0] = divmod(q, 10)
    if q == 9:
    nines += 1
    elif q == 10:
    write(str(predigit+1) + '0'*nines)
    nines, predigit = 0, 0
    else:
    if started or predigit or nines:
    write(str(predigit) + '9'*nines)
    if not started:
    sys.stdout.write('.')
    started = True
    nines, predigit = 0, q
    write(str(predigit) + '\n')
    3.1415926535897932384626433832795028841971693993751058209749445
    923078164062862089986280348253421170679821480865132823066470938
    446095505822317253594081284811174502841027019385211055596446229
    489549303819644288109756659334461284756482337867831652712019091
    456485669234603486104543266482133936072602491412737245870066063
    155881748815209209628292540917153643678925903600113305305488204
    665213841469519415116094330572703657595919530921861173819326117
    931051185480744623799627495673518857527248912279381830119491298
    336733624406566430860213949463952247371907021798609437027705392
    171762931767523846748184676694051320005681271452635608277857713
    427577896091736371787214684409012249534301465495853710507922796
    892589235420199561121290219608640344181598136297747713099605187
    072113499999983729780499510597317328160963185950244594553469083
    026425223082533446850352619311881710100031378387528865875332083
    814206171776691473035982534904287554687311595628638823537875937
    51957781857780532171226806613001927876611195909216420198

    View Slide

  20. 20
    Hope this was fun
    Completing the Circle

    View Slide