Project Euler 94 - Almost Equilateral Triangles

Official link: https://projecteuler.net/problem=94

Thought Process

Consider the above triangle to the right where C is the 2 sides and 2A = C +- 1

B is easily calculated by sqrt(C^2 - A^2)

We need the area of the triangle to be an integer, Area = 1/2 * 2AB = AB

We know 2A is always an integer => A is also an integer (because 2A is even)

Therefore we need to find when B is an integer

  1. Case 1

    • Let 2A = C - 1 => A = (C-1)/2 => Area = A*sqrt(C^2 - (C-1)^2 / 4) = A*sqrt((3C^2 + 2C - 1) / 4) => (3C^2 + 2C - 1) / 4 = k^2

    • Finally, 3C^2 + 2C - 1 = 4k^2

  2. Case 2

    • Let 2A = C + 1 => A = (C+1)/2 => Area = A*sqrt(C^2 - (C+1)^2 / 4) = A*sqrt((3C^2 - 2C - 1) / 4) => (3C^2 - 2C - 1) / 4 = k^2 =>

    • Finally, 3C^2 - 2C - 1 = 4k^2

Using our trusty tool WolframAlpha we can produce integer solutions for C, all we need to do is check whether 3C + 1 or 3C - 1 is greater than 10^9

Interactive Code

Input an integer (yourinput)

Code will output the sum of all perimeter's < yourinput