What is going wrong with my solution for Project Euler 224?

233 Views Asked by At

I am trying to solve the HackerRank version.

The problem statement,

Let us call an integer sided triangle with sides a ≤ b ≤ c barely obtuse if the sides satisfy $a^2 + b^2 = c^2 - 1$.

How many barely obtuse triangles are there with perimeter ≤ $75,000,000$

So the solution that I thought should work is finding the general solution for the above equation, and trying to check the numbers which satisfies all of them.

So this would be $O(n)$ in terms of time efficiency.

The solutions I found are: $a = 2m$, $b = 2m^2$ and $c = 2m^2 + 1$.

Where $ m \ge 1.$

This solution gives correct output for perimeter 21, which should give two triplets $(2,2,3)$ and $(4,8,9)$

But for $75,000,000$ it gives 4329 counts, which is very less than expected.

So now I am not able to understand why some numbers which should be solution are being excluded from this?

I also tried another general solution $a = 2m$, $b = \sqrt{2m}$ and $c = (m+1)$, but it also is giving same outputs as the above one.

I am not able to understand which specific solutions are being skipped in both solutions?

2

There are 2 best solutions below

1
On

Another possible parametrization is $$a=6x^2+4x+2$$ $$b=8x^2+2x+2$$ $$c=10x^2+4x+3$$ giving infinite many solutions which are not of the type you mentioned. Not sure whether we get all solutions , if we add them to your list.

0
On

For $a = 2m, b=2m^2$ and $c = 2m^2 + 1$, you are missing a lot of solutions since $c$ is not always $b + 1$. Ex: $a = 4, b = 8, c = 9$

For the second one, $a = 2m, b = \sqrt{2m}$ and $c = m + 1$, $$ a^2 + b^2 = c^2 - 1$$ $$4m^2 + 2m = m^2 + 2m + 1$$ $$3m^2 = 1$$ $$m = \pm \frac{1}{\sqrt{3}}$$ This is not a general solution for the problem.