Skip to main content

How to Find an Algorithm

For some reason I felt like this was relevant
Happy Pi Day! The digits of the date - 3.14 - contain the first couple digits of π. But that's not all. If you add the year to get 3.14.16, you have the first 5 rounded digits of π. This is better than last year's version (3.14.15), which contained a truncated version of π. Today's date contains the more accurate rounded version.

In this post, I'll describe the process of finding an algorithm to approximate the number π.


There are three main types of algorithms. The first type converges very quickly to the correct solution; we use algorithms of this type in computers and calculators. The second type converges slowly, but is interesting to think about; we take this type of algorithm to mathematics conferences. The third type is slow to converge, and is boring to think about; these are pretty much useless so we get rid of them.

This may be interesting to know, but I wasn't too worried about it when I wrote my Pi Day post last year. I was looking for some simple algorithms, that was all. I needed 5 good algorithms by Pi Day.

A good approach when solving a problem is to start with what you know. What do we know about π? It's the area of a circle with radius 1, circumference of a circle with diameter 1, and it's equal to 180 degrees in radians.

Yes, π can be an angle. As I explained in my 2015 pi day post:

When most people talk about angles, they use degrees – as in 90° (a right angle), 180° (a U-turn), and 360° (1 full turn, or very dizzy). But mathematicians use radians, which involves looking at the circumference of a unit circle, so instead of a full circle being 360°, it's equal to 2π (the circumference of a unit circle). A U-turn, or 180°, is equal to π, and a right-angle (90°) is equal to π/2.


Now that we're looking at π as an angle, the sine and cosine functions come to mind. Sin(π) is zero, and cos(π/2) is zero (see my blog post about sine and cosine). We could use these properties to approximate π. We may choose to go with cos(π) = 0, since sin(0) also equals zero.

An algorithm might look something like this:

1. Start with a = 0
2. Check whether cos(a/2) is close enough to zero
3. If not, try again with another value for a


One important thing to note is that, for a number x slightly greater than π/2, cos(x) < 0. For a number x slightly less than π/2, cos(x) > 0. Based on this knowledge, if cos(a/2) is greater than zero, we need to increase our approximation. If cos(a/2) is less than zero, we need to decrease our approximation. So we could change step 3 to the following (where v is a small number):

3. If cos(a/2) > 0, add v to a. Else add -v to a.

Notice that cos(a/2) has the same sign as the value we're adding. So instead of adding v, why not add cos(a/2)? Here's the final version of the algorithm:

1. Start with a = 0
2. Check whether cos(a/2) is close enough to zero
3. If it's not, add cos(a/2) to a and go back to step 1

Or, for math people, we could write it as a series:

x0 = 0
xn+1 = xn + cos(xn)


Now the important question is, does the algorithm converge? The answer is yes: it converges to π/2. This is because:

error = π/2 - xn > sin(π/2 - xn) = cos(xn)
error = π/2 - xn ≈ cos(xn), for xn close to π/2

Or because:
It's a fixed-point iteration with g'(r) = 0 - namely, the algorithm is of the form xn+1 = g(xn), which will converge to r = g(r) when g'(r) < 1.

Now, of the three types of algorithms I mentioned earlier, which type is this? Well, it converges very quickly, but not efficiently. The function cos(x) is very costly to compute. We clearly wouldn't use this algorithm in a computer. Still, the algorithm is interesting because of its simplicity: it relies only on the properties of the cosine function. Hence, it fits pretty well into the second category.

...on the other hand, if you find math boring, you may feel that the algorithm fits the third category. In which case, today is just a regular Monday. No pi for you.


New posts every month - subscribe for free!

Comments

  1. This is pretty cool! Hi, by the way :-) Dunno if you remember me...

    ReplyDelete
    Replies
    1. Thanks! Yes, I remember you. Been a long time.

      Delete

Post a Comment

Popular posts from this blog

Dividing Paper Puzzle

When I was young, I would fold a sheet of letter paper in half, for origami projects. It occurred to me that the two halves looked almost the same as the whole sheet of paper - except they were smaller. I could see they weren't exactly the same shape; they were off by a little bit. But the idea stuck in my head. You can use a pen, instead of scissors, to halve the paper. Those rectangles all have the same shape, but are different sizes. One night when I was 12, I thought about my idea. I wondered if it was possible to have a sheet of paper that could be cut in half, resulting in 2 smaller versions of the same paper. That would be neat, to be able to cut a paper in half and get 2 papers that had the same exact shape. If that were possible, then you could cut  those  papers, too; and the resulting papers would have the same shape as all the other papers. You could keep cutting in half forever, and each paper, no matter how small, would have the same shape as all the others. I HAD to

Pluto No Longer on the Horizon

This morning, New Horizons became the first spacecraft to make a flyby observation of the Pluto system. During the mission, the spacecraft captured the most detailed photographs of Pluto's surface we've ever had, and possibly ever will have. It also found many new properties including size, mass, atmosphere, and surface composition. In a period of a few hours, we discovered more about Pluto than we've found in the 85 years since Clyde Tombaugh captured its first photograph. Before After  (images credit: NASA) To complete this mission, the spacecraft flew for more than 9 years through the emptiness of space. This may sound like a long time, but it's actually amazingly quick. In fact, New Horizons set the record for the fastest speed at launch, and during the flyby, the spacecraft was moving at a rate of over 30,000 mph, or roughly 50 times the speed of sound. Picture an object twice as heavy as a grand piano moving 25 times faster than a bullet from a gun. Yikes. The man

Gravity

Imagine the universe is filled with water. Instead of empty space, every inch of it contains pure water. No planets, no stars, only water. What happens? And what would happen if an air bubble formed? The answer to this question requires a basic understanding of gravity. Gravity is very important. It helps hold matter together, bends light, and distorts space-time (which, incidentally, is how it bends light). It also makes it possible to play football, and as Americans are big football fans, they would certainly agree that we couldn't live without it. Unfortunately, many Americans don't understand how gravity works. Admittedly, scientists haven't figured out a lot of things, but we do understand it well enough to make predictions and model physical events. One of the important things about gravity is that its strength is proportional to the inverse of the square of the distance. In other words, it gets weaker as you get farther away, based on the equation: F = c/d 2 where c