Monday, March 14, 2016

Using Your Computer to Calculate Pi

On March 21, 2015, Rajveer Meena recited 70,000 digits of pi from memory in just under 10 hours in Vellore, India, for the Guinness World Record of most digits of pi memorized.  Apart from utter amazement by this feat of memory, have you ever wondered how mathematicians can even calculate such a long list numbers for pi?


There are actually several methods for calculating pi.  You might recall from your math classes that pi is the ratio between the circumference and diameter of any circle, and so one way to calculate pi is to construct a circle, directly measure its circumference and diameter, and divide the two.  Unfortunately, this is only precise enough to obtain a few decimal places of pi.  Another way to calculate pi is to find the perimeter or areas of inscribed or circumscribed polygons of circles.  This was the technique favored by mathematicians before the invention of calculators and computers, but its precision is still limited to “just” a few hundred digits of pi.

inscribed octagon

Today, computers can be used to calculate millions of digits of pi by using infinite series formulas.  There are several different ways to do this, but one of the most efficient methods that is relatively straightforward is to combine Machin’s formula:


with the arc-cotangent power series formula:


The computer program for this is as follows:

01
# Pi Calculator
02
# Python 2.7.3
03
# After running, type "pi(n)" where n is the number of decimals for pi.  For
04
#  example, if you would like to calculate 100 decimals for pi, type "pi(100)".
05

06
# import python libraries
07
from decimal import Decimal, getcontext
08
from time import time, strftime
09
import datetime
10

11
# arccot function using power formula arccot = 1/x - 1/(3x^3) + 1/(5x^5) ...
12
def arccot(x, digits):
13
    # set precision and starting values
14
    getcontext().prec = digits
15
    total = 0
16
    n = 1
17
    # loop while new term is large enough to actually change the total
18
    while Decimal((2 * n - 1) * x ** (2 * n - 1)) < Decimal(10 ** digits):
19
        # find value of new term
20
        term = ((-1)**(n - 1)) * 1 / Decimal((2 * n - 1) * x ** (2 * n - 1))
21
        # add the new term to the total
22
        total += term
23
        # next n
24
        n += 1
25
    # return the sum
26
    return total
27

28
# pi function
29
def pi(decimals):
30
    # start timer
31
    timestart = time()
32

33
    # find pi using Machin's Formula pi = 4 * (4 * arccot(5) - arccot(239))
34
    #  and the power formula for arccot (see arccot function above)
35
    print "pi = " + str(Decimal(4 * (4 * arccot(5, decimals + 3) - arccot(239,
36
        decimals + 3))).quantize(Decimal(10) ** (-decimals)))
37

38
    # display elapsed time
39
    timeelapsedint = round(time() - timestart, 2)
40
    timeelapsedstr = str(datetime.timedelta(seconds = round(
41
        timeelapsedint, 0)))
42
    print "runtime: " + timeelapsedstr + " or " + str(
43
        timeelapsedint) + " seconds."

Here’s what happens when you use the program to find the first 1,000 decimal places for pi on a dual-core 1.67 GHz processor (a mediocre-speed laptop for 2016):

>> pi(1000)
pi = 3.14159265358979323846264338327950288419716939937510
582097494459230781640628620899862803482534211706798214808
651328230664709384460955058223172535940812848111745028410
270193852110555964462294895493038196442881097566593344612
847564823378678316527120190914564856692346034861045432664
821339360726024914127372458700660631558817488152092096282
925409171536436789259036001133053054882046652138414695194
151160943305727036575959195309218611738193261179310511854
807446237996274956735188575272489122793818301194912983367
336244065664308602139494639522473719070217986094370277053
921717629317675238467481846766940513200056812714526356082
778577134275778960917363717872146844090122495343014654958
537105079227968925892354201995611212902196086403441815981
362977477130996051870721134999999837297804995105973173281
609631859502445945534690830264252230825334468503526193118
817101000313783875288658753320838142061717766914730359825
349042875546873115956286388235378759375195778185778053217
12268066130019278766111959092164201989
runtime: 0:00:04 or 3.73 seconds.
  
So with less than 50 lines of code, you can have your computer calculate 1,000 decimal places of pi in less than 4 seconds!  At this rate, you will have 1,000,000 decimal places of pi in just over an hour!  Not bad, considering the very first computer, the ENIAC, took 70 hours to calculate pi to 2,037 decimal places in 1949.  Computers have come a long way since then!


With better computers and more efficient (but more complicated) infinite series formulas, pi has been calculated to over 13.3 trillion digits!  This number is unfathomable to most of us.  If you were to recite this many digits of pi at the same speed as the 2015 world record holder Meena (about 2 digits per second), it would take you over 200,000 years to finish!  So why bother to find so many digits of pi?   Most mathematicians will give the same reason that mountain climbers use for climbing Mount Everest: because it’s there.


Thursday, March 10, 2016

Bouncing DVD Logo

In the TV show The Office, there is a humorous scene where all the employees, who are supposed to be paying attention to their boss who is leading a meeting, are watching the bouncing DVD logo on the television screen behind him instead.  They are all hoping that the bouncing DVD logo will move in and out of one of the exact corners of the television screen, and show disappointment every time the logo comes close but just misses it.  Meanwhile, their boss, oblivious to all of this, thinks that all of their reactions to the bouncing DVD logo are actually reactions to his agenda.


The bouncing DVD logo follows a path common to many digital devices, including the Roku logo, Pandora’s musical information, and other computer screensavers.  The logo starts moving diagonally from one corner of the screen, and any time it gets to the edge of the screen it “bounces” off and changes direction, like a ball hitting a wall.  Most of the time the logo only changes directions at a 90° angle when it hits one of the sides of the screen, but on some rare occasions it hits one of the corners exactly and changes directions at a 180° angle instead.  This begs the question: can we mathematically predict when the logo will hit one of the corners?


Let us first consider some simplified examples, and then draw some generalizations from there.  Here is a 1 x 1 pixel logo moving inside of a 16 x 10 pixel screen, where the total distance traveled by the logo is recorded in the bottom right:


It should be observed that the logo hits the top and bottom walls at every multiple of 9 (d = 9, 18, 27, 36, and 45) and the left and right walls at every multiple of 15 (d = 15, 30, and 45).  The logo therefore hits the corner (the intersection of a top/bottom wall with a left/right wall) at the least common multiple of 9 and 15, or at d = 45. 

But why the numbers 9 and 15?  Shouldn’t it be 10 and 16, since that’s the dimensions of the screen?  If we follow the point at the exact center of the logo, we need to notice that it never gets within half the logo’s height to the top and bottom walls, nor within half the logo’s width to left and right walls.  Therefore, the movement of the logo is restricted to these half-logo margins around the screen, which means from the screen’s height we need to subtract half the logo’s height (for the top margin) and half the logo’s height (for the bottom margin), which is the same as subtracting the whole logo’s height.  Likewise, from the screen’s width we need to subtract half the logo’s width (for the left margin) and half the logo’s width (for the right margin) which is the same as subtracting the whole logo’s width.  In this case, 9 is the difference of heights of the screen and logo (10 – 1 = 9), and 15 is the difference of widths of the screen and logo (16 – 1 = 15).

Therefore, a bouncing logo will reach a corner at the least common multiple of the difference of the screen and logo heights and the difference of the screen and logo widths, or

d = LCM(hs – hl, ws – wl)

We can test our equation by looking at a bigger 4 x 2 pixel logo moving inside the same 16 x 10 pixel screen:


According to our formula, d = LCM(hs – hl, ws – wl) = LCM(10 – 2, 16 – 4) = LCM(8, 12) = 24.  Sure enough, we observe that the logo hits the top and bottom walls at every multiple of 8 (d = 8, 16, and 24) and the left and right walls at every multiple of 12 (d = 12 and 24), and the logo hits its first corner at d = 24.

Another way to explain the bouncing logo equation is to picture its path as a straight line through a series of reflections.  When the logo hits the top or bottom wall, a horizontal mirror along the half-height-logo margin would reflect its rebounded path in a straight line to the original.  Likewise, when the logo hits the left or right wall, a vertical mirror along the half-width-logo margin would reflect its rebounded path in a straight line to the original.  Here’s what the 1 x 1 logo moving inside of a 16 x 10 screen would like in a straight line using a series of reflections:


We can see that the logo will hit the corner when the reflected screens can be combined to make a square.  Since the size of each reflected screen is the difference of the screen and logo heights and the difference of the screen and logo widths, and since a square will only be formed by a least common multiple, we have verified the formula d = LCM(hs – hl, ws – wl).

Since the logo travels at a constant speed, a proportion can be set up using the formula v = d/t to give a formula for the time it will take for the logo to travel from one corner to the next.  In other words, the distance and time of one cycle is proportionate to the distance and time of one screen length:


which can be algebraically rearranged into the following helpful formula:


This means that the time it takes for a bouncing logo to cycle through one corner to the next can be calculated with five variables: the time it takes for it to move across one width of the screen, the screen height, the screen width, the logo height, and the logo width.

So using estimates from the video footage of The Office episode, we can calculate that it takes 6 seconds for the bouncing DVD logo to go one width of the screen, and if the TV screen is 800 x 600 pixels, the logo is about 140 x 140 pixels.  Therefore, tcycle = tws – wl · LCM(hs – hl, ws – wl) / (ws – wl) = 6 · LCM(600 – 140, 800 – 140) / (800 – 140) = 6 · LCM(460, 660) / 660 = 6 · 15180 / 660 = 138 seconds.  In other words, the DVD logo will hit a corner of the television screen every 2 minutes and 18 seconds.


Unfortunately, since the equation uses a least common multiple function, and since the least common multiple function is not a continuous function, estimates may not be accurate.  Previously, we estimated the logo to be 140 x 140 pixels which resulted in a calculated cycle of 2 minutes and 18 seconds.  However, if the logo was really 141 x 141 pixels, the calculated cycle would be over 45 minutes, or if the logo was really 139 x 139 pixels, the calculated cycle would be over 46 minutes!  The following is a table of calculated cycles for logos from 135 x 135 pixels to 145 x 145 pixels:

Logo Size
(pixels)
Cycle Time
(min)
135 x 135
09:18
136 x 136
05:48
137 x 137
46:18
138 x 138
23:06
139 x 139
46:06
140 x 140
02:18
141 x 141
45:54
142 x 142
22:54
143 x 143
45:42
144 x 144
05:42
145 x 145
09:06

Therefore, the time it takes for a bouncing logo to move from one corner to the next can be predicted using five variables: the time it takes for it to move across one width of the screen, the screen height, the screen width, the logo height, and the logo width.  However, given the nature of the least common multiple function, these variables must be known exactly or the calculation will not be accurate.  Also, every television and logo size are different, so answers will vary.