Every March the 14th we celebrate Pi Day (3/14). It's a special day for us because we love math(s), and programming, and the ‘Pi’ in Raspberry Pi is short for Python. So says Eben:
Pi is because originally we were going to produce a computer that could only really run Python. So the Pi in there is for Python. Now you can run Python on the Raspberry Pi, but the design we ended up going with is much more capable than the original.
So this year we thought we'd dig a little deeper into pi and learn a little more with our favourite computer.
Calculate pi in Python (on a Raspberry Pi)
It's actually pretty easy to access pi on a Raspberry Pi. Open Python (choose Menu > Programming > Python 3 (IDLE) ) and enter the following:
from math import pi
pi
This will return pi to 15 decimal places:
3.141592653589793
Sure, that's pretty easy. But how is pi calculated in the first place? And what if we wanted to calculate pi to more decimal places?
For that we need a more detailed program. Open File > New and save the file as ‘pi.py’. Now enter the following code (credit: Martin Thoma, Stack Overflow):
import decimal def pi(): """ Compute Pi to the current precision. Examples -------- >>> print(pi) 3.141592653589793238462643383 Notes ----- Taken from https://docs.python.org/3/library/decimal.html#recipes """ decimal.getcontext().prec += 2 # extra digits for intermediate steps three = decimal.Decimal(3) # substitute "three=3.0" for regular floats lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 while s != lasts: lasts = s n, na = n + na, na + 8 d, da = d + da, da + 32 t = (t * n) / d s += t decimal.getcontext().prec -= 2 return +s # unary plus applies the new precision decimal.getcontext().prec = 1000 pi = pi()
Press F5 to run the program and enter:
print(pi)
And you'll get pi to 1000 decimal places. Much more pi than math.pi! Change the .prec value to get even more detail.
decimal.getcontext().prec = 2000
This code uses Issac Newton's formula for calculating Pi. You can learn more about it at Pi314.
Learn pi with Khan Academy
We can't recommend Khan Academy highly enough. Khan Academy is a non-profit educational organisation, and all of its videos and interactive lessons are free.
Salman Khan created the site in 2008, and his calm voice walks through mathematical concepts while he sketches on the screen using SmoothDraw 3 and a Wacom Bamboo tablet.
All of the videos are available on YouTube and, far from being a dry, disinteresting lecture, they quickly capture the joy of mathematics.
The circle is arguably the most fundamental shape in our universe, whether you look at the shapes of orbits of planets, whether you look at wheels, whether you look at things on kind of a molecular level. They kept measuring it better and better and better, and then they realised that they were getting this number: 3.14159. And they just kept adding digits and it would never repeat. It was a strange fascinating metaphysical number. And it just showed up for every circle. The ratio of the circumference of the diameter was this kind of magical number. So they gave it a name; they called it ‘pi’.
Many people (including us) drop into Khan Academy on a regular basis to brush up on a math(s) subject. But Salman Khan also creates fabulous resources such as this Discovering Pi document (developed with NASA).
Happy Pi Day!