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

2014 in Photos

The year of 2014 is over; the year of 2015 has begun. Over the past year, I took a lot of pictures - more than 1200 photos of clouds, bugs, plants, rocks, and more. I deleted most of the low-quality and repetitive photos. Then, I selected 20 of the remaining photos which I thought were the most interesting or significant. In this post, I'll include those photos, in chronological order. Spring 1. Cold Hummingbird I took this photo after a late snowstorm in Spring. In the days leading up to the storm, it was warm and sunny and the fields were green, so the snow came rather unexpectedly. During the storm, one of the hummingbirds at our feeder was covered in ice and could barely fly. This hummingbird was more fortunate, but unlucky nevertheless. 2. Green Landscape I took this photo on the side of a biking trail. I didn't edit or crop the photo - this is how it looked. 3. Indian Paintbrush The Indian Paintbrush is an unusual flower. What appear to be flower petals are actually brigh...

Nature Wallpaper

I collected 12 of the highest-resolution, best-quality photographs of nature I've taken over the past few years, cropped them to highlight the important parts, and then applied JPEG compression. Now I'm releasing them for my blog readers to enjoy as a desktop background. To download as a .zip file, click here . Note that the photos shown in this post are low-resolution previews. If you have any questions about the wallpaper, please comment! New posts every month -  subscribe for free !

Which Hurts More?

212° F Let's play a little game. I'll list a bunch of possible actions. Each action will have 2 variations, (a) and (b). You choose either (a) or (b), depending on which would be safer (or less painful). Each of the questions will involve an oven hot enough to bake a cake (350° F), and a pot of boiling water (assume we're at sea level). So... would you rather: 1.     (a) Stick your hand in the oven     (b) Stick your hand in the boiling water   ... for a period of 10 seconds 2.     (a) Leave a fork in the oven     (b) Leave a fork in boiling water   ... for a period of 15 minutes. Then hold the fork tight with your bare hand. 3. Fill a jar to the top with cool tap water. Then:    (a) Place the jar in the oven    (b) Place the jar in the boiling water   ... for a specific, but unknown, period of time. Then remove the jar and put your hand in it. First see if you can figure these out yourself. They shouldn't be too...