Problem 247: Squares under a hyperbola

Let S_1 be the largest square that can fit under the curve.
Let S_2 be the largest square that fits in the remaining area, and so on.
Let the index of Sn be the pair (left, below) indicating the number of squares to the left of S_n and the number of squares below S_n.

The diagram shows some such squares labelled by number.S_2 has one square to its left and none below, so the index of S_2 is (1,0).
It can be seen that the index of S_32 is (1,1) as is the index of S_50.
50 is the largest n for which the index of S_n is (1,1).

What is the largest n for which the index of S_n is (3,3)?

My Algorithm

Each square has a lower-left corner (x_0,y_0) and an upper-right corner (x_1,y_1) such that:(1)x_1 - x_0 = y_1 - y_0 = side (length of each side of that square)

The hyperbola y = 1/x must go through the upper-right corner:(2)y_1 = dfrac{1}{x_1}

My program starts with a single square whose lower-left corner (x, y) is at (1, 0).
All squares are stored in an std::set named todo which is sorted descendingly by the square's size
(the overloaded Square::operator< returns the "wrong" result on purpose because an std::set is sorted ascendingly by default).

I always pick the largest (→ the first) square from todo and replace it by its upper and right neighbor.
Each square's position (x, y) and index (left, below) are tracked.

In order to have a square at index (3, 3) I have to have a square at (2, 3) or (3, 2).
Generalized, only if some squares are in todo where left <= 3 and below <= 3 and left + below < 3+3 then it's
still possible to generate a square at index (3, 3).candidates counts how many squares fulfil that condition. If candidates becomes zero then no more squares can be at (3,3).
The most recent will be printed.

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 "1 1" | ./247

Output:

(please click 'Go !')

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

#include<cmath>

// a square is described by its lower left corner (x,y) and its index along x and y axis

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 !