Problem 91: Right triangles with integer coordinates

The points P(x1,y1) and Q(x2,y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form a triangle \triangle OPQ.

There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive;
that is, 0 <= x1, y1, x2, y2 <= 2.

Given that 0 <= x1, y1, x2, y2 <= 50, how many right triangles can be formed?

My Algorithm

The location of O was fixed at (0,0) but P and Q can be anywhere (but O!=P!=Q).
To simplify my algorithm I define that the right angle is always at O or P and never at Q.
The size of the grid is 50x50, each side has length size=50.

I identified 4 cases:
1. The right angle is at the origin O. P and Q can be anywhere on the x-/y-axis:P(0<p_x<=size, 0) and Q(0, 0<q_y<=size) → that's size^2 different triangles.

2. The right angle is on the x-axis at P(0<p_x<=size, 0). Any Q with the same x-value makes a valid triangle:P(0<p_x<=size, 0) and Q(p_x, 0<q_y<=size) → that's size^2 different triangles.

3. The right angle is on the y-axis at P(0, 0<p_y<=size). Any Q with the same y-value makes a valid triangle:P(0, 0<p_y<=size) and Q(0<q_x<=size, p_y) → that's size^2 different triangles.

4. The right angle is "inside" the grid at P(0<p_x<=size, 0<p_y<=size).

The fourth case is a more challenging case and will be discussed in detail:
The right angle is enclosed by two vectors \vec{a} = \vec{PO} and \vec{b} = \vec{PQ}.
If they are perpendicular \vec{a} \bot \vec{b}, then their dot product is zero: \vec{a} \cdotp \vec{b} = a_{x}b_x + a_{y}b_y = 0

Two nested for-loops produce all integer combinations of 0 < p_x <= size and 0 < p_y <= size.
We only want integer solutions for q_x and q_y, too.
But if k is an integer, too, then we would miss some valid Q - e.g. for P(2,2) and Q(3,1) it follows that k=frac{1}{2}.
We get all integer solutions if k is a multiple of dfrac{1}{gcd(p_x,p_y)} (the Greatest Common Divisor, see en.wikipedia.org/wiki/Greatest_common_divisor)
For P(2,2) we get dfrac{1}{gcd(2,2)}=dfrac{1}{2}

Enough mathematics, let's dive into the code ... cases 1 to 3 don't need much of an explanation.
Then follows two nested for-loop that analyze each P(p_x,p_y) of case 4.factor is the gcd(p_x,p_y) such that k_{n+1} = k_n + 1/factor
The point Q is found by repeated adding p_y/factor to p_x and subtracting p_x/factor from p_y (for k > 0)
until any coordinate leaves the grid. The same procedure is done with inverse sign for k < 0.

A simple speed-up was achieved by observing that the number of valid Q is the same for P(p_x, p_y) and P(p_y, p_x).
That means that the two nested loops only need to considered the bottom-right triangle of the grid: P(p_x, p_y<=p_x).
For P(p_x, p_y<p_x) I multiply the number of Qs by 2 - but not when p_x=p_y.

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)

Changelog

Hackerrank

Difficulty

25%
Project Euler ranks this problem at 25% (out of 100%).

Hackerrank describes this problem as medium.

Note:Hackerrank has strict execution time limits (typically 2 seconds for C++ code) and often a much wider input range than the original problem.In my opinion, Hackerrank's modified problems are usually a lot harder to solve. As a rule thumb: brute-force is rarely an option.

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 !