One way to approximate π is the following: Start by drawing a 2x2 square with a quarter-circle in it. Then, the area of the quarter-circle is π.
We can approximate this area by filling it with squares. To start, we divide the square into four squares. Only one of these is fully inside the circle, so we colour it in and get a value of π = 1.
Next, we repeat the same thing with the other three squares, dividing them into quarters and colouring those fully contained in the quarter-circle. This gives a value of π = 2.
And again, with π = 41/16 ≈ 2.56:
And again, with π = 183/64 ≈ 2.86:
Once more, with π = 385/128 ≈ 3.007:
And one last time, because these are pretty, with π = 3149/1024 ≈ 3.075:
Your challenge is to count the squares in each iteration of this pattern. The first pattern has one red square (total 1), then the next pattern has four orange squares for a total of 5, then the next has nine yellow squares for a total of 14, and so on. The sequence continues 1, 5, 14, 33, 71, 140, 274, 563, 1083...
- here's an example python program to calculate the first few terms.
While this specific sequence is not on OEIS, its variations A156790 and A177144 are. (incidentally, this is what you get if you search the first few terms of the sequence). Standard sequence I/O rules apply - you may take a 0/1-indexed n
and output the n
th / first n
terms, or just output the whole sequence indefinitely.
This is code-golf, shortest wins!