Problem 199: Iterative Circle Packing

Three circles of equal radius are placed inside a larger circle such that each pair of circles is tangent to one another and the inner circles do not overlap.
There are four uncovered "gaps" which are to be filled iteratively with more tangent circles.

At each iteration, a maximally sized circle is placed in each gap, which creates more gaps for the next iteration.
After 3 iterations (pictured), there are 108 gaps and the fraction of the area which is not covered by circles is 0.06790342, rounded to eight decimal places.

What fraction of the area is not covered by circles after 10 iterations?
Give your answer rounded to eight decimal places using the format x.xxxxxxxx .

My Algorithm

Two days ago I published my solution to problem 510 where I mentioned that I got the final insights to solve 510 and 199 by going to a museum:
by pure chance I saw some ancient drawings that looked pretty much like the circles in the example.
And they had a name: Apollonian gasket (see en.wikipedia.org/wiki/Apollonian_gasket)

So when there are four circles touching each other the their "curvatures" k always fulfil that equation.
The curvate is defined to be k = 1/r and, even more interesting, it can be negative when a circle touches the other with its inside.
That's only the case for the outermost circle.

If three curvatures are known then (1) can be transformed to compute the fourth:(2)k_4 = k_1 + k_2 + k_3 \pm 2 sqrt{k_1 k_2 + k_2 k_3 + k_1 k_3}

I started with the three circles at level 1 and defined their radius to be 1. I found two circles: one between the three and one outside the three.
The outermost circle (level 0) must be the one with the larger radius which turned out to be 3 - 2 sqrt{3} approx 2.1547.
Since it contains all other circle its curvature must be negative: k = - dfrac{1}{3 - 2 sqrt{3}}

My function evaluate() recursively determines the the sum of the area covered by the "children" of three circles
with curvatures k1, k2 and k3 up to recursion level depth.
There are two distinct groups of "parents":

three V-shaped areas enclosed by the outermost circle and two circles at level 1

the small area between the three circles at level 1

There is no need to compute each V-shaped area on its own: they are identical and I only need to compute one (and multiply by 3).

Now I know the area of all children (up to level 10). I haven't added the area of the three circles at level 1 yet.
Their sum divided by the area of the outermost circle gives the percentage of coverage, and 100% minus this value is the final result.

Note

evalute() will often be called with the same parameter set and becomes pretty slow beyond level 16.
I tried to add a small memoization scheme (same trick as always ...) but ended up slowing the the program for the default input 10 considerably.
Therefore I removed it - even though it's more efficient for higher input values.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent toecho 3 | ./199

Output:

(please click 'Go !')

Note: the original problem's input 10cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

Benchmark

The correct solution to the original Project Euler problem was found in less than 0.01 seconds on an Intel® Core™ i7-2600K CPU @ 3.40GHz.
(compiled for x86_64 / Linux, GCC flags: -O3 -march=native -fno-exceptions -fno-rtti -std=gnu++11 -DORIGINAL)

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !