Problem 587: Concave triangle

A square is drawn around a circle as shown in the diagram below on the left.
We shall call the blue shaded region the L-section.
A line is drawn from the bottom left of the square to the top right as shown in the diagram on the right.
We shall call the orange shaded region a concave triangle.

It should be clear that the concave triangle occupies exactly half of the L-section.

Two circles are placed next to each other horizontally, a rectangle is drawn around both circles, and a line is drawn from the bottom left to the top right as shown in the diagram below.

This time the concave triangle occupies approximately 36.46% of the L-section.

If n circles are placed next to each other horizontally, a rectangle is drawn around the n circles, and a line is drawn from the bottom left to the top right, then it can be shown that the least value of n for which the concave triangle occupies less than 10% of the L-section is n = 15.

What is the least value of n for which the concave triangle occupies less than 0.1% of the L-section?

My Algorithm

This is a so-called paper'n'pen problem because you can solve it without a computer.
But I like computers so much that I decided to write a proper program that solves this problem by throwing CPU cycles at it !

A little mathematics is still required:

the line from the lower left corner to the upper right corner has the equation y = mx where m is the slope

I define that my circles have a radius r = 1

so that the box has a height of r+r = 2

If there is only one circle then it covers an area of pi r^2 = pi 1^2 = pi. Its bounding box covers an area of 2^2 = 4.
There are four L-shaped areas between circle and box - all have the same size, so that one L-shaped area covers an area of(1)(4 - pi) / 4 approx 0.2146

To find the intersection between the line and the first circle - which is centered at (1,1) - I solve both equation for y:(2)y = mx(3)(x - 1)^2 + (y - 1)^2 = 1(4)(y - 1)^2 = 1 - (x - 1)^2(5)y - 1 = \pm sqrt{1 - (x - 1)^2}(6)y = 1 - sqrt{1 - (x - 1)^2} ← I only need the lower half of the circle where the first intersection takes place

The intersection is at (2) = (6):(7)mx = 1 - sqrt{1 - (x - 1)^2}

And that's when I decided to write a "true" IT solution and solve equation (7) numerically:

compute left and right side of equation (7)

if they are "close enough", then abort

else move x accordingly

start with large moves and then make smaller moves

see getIntersection() (where slope is m)

Knowing the intersection I proceed with the same motivation: keep solving it numerically !

function getAreaL() computes the area of the L-shaped area given a slope

left of the intersection point is a triangle with area x * y / 2 = x * m x / 2

At first I checked every possible size of 1,2,3,...,100,... circles whether the area is below 0.1%.
It took a little more than one second to find the correct solution.
Knowing that the area is strictly decreasing I re-wrote my code to work similar to binary intersection:
check only every 64th number: 64, 128, 192, ... circles and when the area is below the limit then go back and check every 32nd, then 16th, ... until step = 1.
Now the result is printed after less than 0.1 seconds.

Alternative Approaches

As I told you before, you can solve this with pen'n'paper (or Wolfram Alpha).

Note

Of course there are a few assumptions which are not always true:

maybe Epsilon is too large and the result will be wrong

maybe I need more than 100000 steps to accurately evaluate getAreaL

→ but my default values easily solve the problem

I had fun writing this solution - for whatever reason I like numerical approaches such as Monte-Carlo simulations.

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):Note: Enter the percentage of the L-shape

This is equivalent toecho 10 | ./587

Output:

(please click 'Go !')

Note: the original problem's input 0.1cannot 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.

#include<iostream>

#include<cmath>

constauto Epsilon = 0.00000000001;

constauto NoLine = 0;

// return x where line and first circle intersection

doublegetIntersection(double slope)

{

// circle: (x-1)^2 + (y-1)^2 = 1

// (y-1)^2 = 1 - (x-1)^2

// y-1 = sqrt(1 - (x-1)^2)

// y = 1 - sqrt(1 - (x-1)^2)

// line: y = mx <= where m is the slope

// their intersection: mx = 1 - sqrt(1 - (x-1)^2)

// I entered those formula in Wolfram Alpha and didn't like the output ... so let's solve this numerically !

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 !