Calculating Pi With a Raspberry Pi

2.2K71

Intro: Calculating Pi With a Raspberry Pi

Pi is defined as the ratio of a circle's circumference to its diameter. For Pi Day, March 14, Halifax Makerspace assembled a small, crack team of makers to calculate this intriguing and irrational constant. There was Tanner, an accomplished fine woodworker and physicist turned professional programmer, Allan, a full-time multidisciplinary maker and marketer, and Shawn, who had a basement to film in.

STEP 1: Choosing a Raspberry Pi

The Raspberry Pi is a full computer on a single board. It's been around a few years, and has caught on as the go-to single board computer among the maker community. We had several models of Raspberry Pi to choose from, but narrowed it down to a Pi v1 Model A and Pi v3 Model B.

Pi 3 Model B vs. Pi 1 Model A

  • 1200MHz quad core vs. 700MHz single coreprocessor
  • 1Gb RAM vs. 256 Mb RAM
  • 802.11n & Bluetooth vs. no on-board wifi
  • 2.5A vs. 700mA power draw

In the end, we chose the Pi 1, but you should be able to use any model. We installed NOOBS on it, the operating system preferred by Pi users, but this is optional.

STEP 2: The Mathematical Underpinnings of Our Approach

The approach we took to calculating pi derives from the work of 18th century French mathematician, Georges-Louis Leclerc, Comte de Buffon. He posited that, if you were to lay out a series of equidistant parallel lines and drop a large number of needles onto this layout, the randomness of each needle's distance from the line closest to it and randomness of its rotational position could be used to approximate pi. In fact, you don't need to record the position and rotation: you can simply record the number of needles that cross a line.

The equation is:

pi = 2ln / th

where:

  • l = length of needles
  • n = number of needles dropped
  • t = distance between lines
  • h = number of needles that cross a line

So all we need to do is generate a large set of random data with the Raspberry Pi, plug the results into the formula, and we get an approximation for pi!

STEP 3: Generating Random Data With the Pi

We laid out a series of parallel lines 3" apart on the workbench and spent an hour throwing the Raspberry Pi onto it. Every time it landed, we'd use a marker to trace an end of the Pi onto the paper, using different colours depending on whether it crossed a line or not. After about 200 throws, we stopped and counted each colour.

STEP 4: Results!

In the end, we had done 176 trials. 86 of those crossed a line.

Plugging those numbers into our formula from earlier, we get:

  • l = 2.25
  • n = 186
  • t = 3
  • h =90

pi = 2ln / th
pi = (2 x 2.25 x 176) / (3 x 86)
pi = 3.07

So the experimental value we get for pi is 3.07. This is pretty darn close to the accepted approximation of 3.14. Had it not been getting late, and had we not already worked through lunch, we likely could have done more trials and gotten even closer.

In the future, we feel this methodology could be applied to Arduino Megas, T1 graphing calculators and perhaps Dell laptops, but more funding is needed to continue this research.

FYI: the Pi experienced a broken capacitor after about 100 throws, and a couple bent pins. Other than that, we were pretty impressed with its performance.

Comments

Brilliant!

This is actually very similar to my forthcoming instructable of how to find the height of a building using a Raspberry Pi.

I am currently favouring the 'Tie a Raspberry Pi to a long piece of string and dangle it off the building until it touches the floor then measuring the string' method.

My colleage is preferring to attempt the 'Find the buildings caretaker and ask if he will tell us the height of the building in exchange for a Raspberry Pi' technique.

I am unsure about the latter method myself as it could turn out to be Pi model specific depending on how hard a bargain the caretaker drives.