Warning: I don’t think the stuff in this post has any direct practical application by itself (unless you’re a nuclear war survivor and need to reconstruct maths from scratch or something). Sometimes I like to go back to basics, though. Here’s a look at and areas of curved shapes without any calculus or transcendental functions.
A simple algorithm for calculating
This algorithm starts with simple number theoretic musing. Some whole numbers form neat Pythagorean triples where . E.g., . It’s easy to find all the solutions to through brute-force search because we know that and can’t be bigger than . Here they are:
(Plus all the negative-number combinations, but let’s stick with non-negative integers and just count 4 solutions.) If we relax the equation, and count solutions to , the answer turns out to be 26. Why care? Well, if is the total number of solutions to , then
Or, in code, here’s a simple program that estimates , getting more accurate for bigger values of the n
variable:
Okay, that’s a little bit more accurate than . Unlike most formulae for , though, there’s a simple diagram that shows how it works. Imagine we lay out the integer pairs (where and range from to ) on a 2D grid the obvious way. The figure below shows an example for , with the arrow pointing from the origin to . and the and components make a right-angled triangle, so Pythagoras’s theorem says that . For , , so is on the boundary as a solution to . That boundary (the set of real-valued points a constant distance from the origin) makes a quarter circle.
A circle is a simple, convex shape, and the grid points are evenly spaced, so the number of points inside the quarter circle will be roughly proportional to the area. More specifically, the fraction of all the grid points inside the quarter circle will be roughly the area of the quarter circle divided by the area of square around all points. The quarter circle area is , inside the square of area (remember, ), so of all points represent solutions. The and values count from to , so there are grid points. Rearrange the equations and you get a formula for estimating from a solution count. The grid points keep drawing an arbitrarily more accurate circle as gets bigger (just like a higher-resolution computer monitor does) so the estimate is exact in the limit.
A faster implementation
The code above is simple but slow because it brute-force scans over all possible and values. But we obviously don’t need to scan all values. If we know that , then making or smaller will only give us another solution. We don’t need to keep testing smaller values after we find a solution. Ultimately, we only need to find the integral points around the boundary. Here’s a faster algorithm based on that idea.
Imagine we scan along the integral values and find the maximum integral value that still gives us a solution. This gives us a border line marked in red in the figure below. If for a given value, we instantly know there are solutions with that given value ( to count the solution).
Note that as scans from to , starts at and decreases to . Importantly, it only decreases — it’s monotonic. So if we scan from to , we can find the next boundary point by starting from the previous boundary point and searching downwards. Here’s some code:
This version still has nested loops, so it might look like it’s still . However, the inner while
loop executes a
varying number of times for each value. Often the y--
doesn’t trigger at
all. In fact, because y
starts from n
and monotonically decreases to 0, we know the y--
will be executed exactly n
times in total. There’s no instruction in that code that executes more
than times, total, so the whole algorithm is .
With 64b ulong
integers, the largest value of n
that works before overflow is 4294967294:
There are ways to get faster convergence using numerical integration tricks, but I like the way this algorithm only uses integer arithmetic (up until the final division), and can be understood directly from simple diagrams.
Area of a circle without calculus
Perhaps you feel a bit cheated because that algorithm assumes the formula for the area of a circle. Sure, that’s arguably included in “high school maths”, but it’s something students just get told to remember, unless they study integral calculus and derive it that way. But if we’re going to assume , why not assume the theory of trigonometric functions as well, and just use ?
The great ancient Greek mathematician Archimedes figured out the circle area over two thousand years ago without integral calculus (or trigonometric functions for that matter). He started with an elegant insight about regular (i.e., equal-sided) polygons.
The famous “half base times height” formula for the area of a triangle already had a well-known proof in the first book of Euclid’s Elements of Geometry (easily derived from a theorem about parallelograms). Conveniently, any regular polygon can be split into equal triangles joined to the centre. For example, a regular hexagon splits into six triangles, as in the figure below. We can take any one of the triangles (they’re all the same) and call the “base” the side that’s also a side of the polygon. Then the “height” is the line from the centre of the base to the centre of the polygon.
Now here’s Archimedes’s neat insight: The ratio of the triangle area to the base is . If you add up all the areas, you get the area of the polygon. Likewise, if you add up all the bases, you get the perimeter of the polygon. Because the triangle area/base ratio is a constant for all triangles, the area/perimeter ratio for the whole polygon is the same . As a formula, the area of any regular polygon is (where is the perimeter).
If you think of a circle as a regular polygon with infinitely many sides (so that becomes the radius of the circle), and use the circle circumference () as your basic definition of , then that implies the area of a circle is .
Of course, Archimedes was a respected mathematician who couldn’t get away with just assuming that anything true of a polygon is true of a circle (counterexample: all polygons have bumpy corners, but circles don’t). He used the kind of geometric proof by contradiction that was popular in his day. (He even took it further and analysed spheres, cylinders, parabolas and other curved objects, almost inventing something like modern real analysis a couple of millenia early.) Sadly, not all of his mathemetical work has survived, but the key part of his Measurement of a Circle has.
Here’s the high-level version. Archimedes claimed that the area of a circle is . Suppose you think his value is too small, and the circle is really bigger than . That means there’s enough room inside the circle to fit a regular polygon that’s also bigger than . But Archimedes said that’s contradictory because for any such polygon, , and (because each side of the polygon is a straight line that’s shorter than the circle arc that connects the same points), so the area . The polygon’s area can’t be both bigger and smaller than .
Archimedes argued that there’s a similar contradiction if you think is too big, and the circle area is smaller than that. In that case he could make a polygon that’s also smaller than , yet still wraps around the circle. For this polygon, , but he said the perimeter of the polygon must be greater than 1, so that the polygon’s area must be bigger than , even though it’s also meant to be smaller.
If both of those cases lead to contradiction, we’re left with the only alternative that the circle area is .
-
I don’t actually know how he argued this. ↩