Pythagoras' Other Leg

Jelly, 8 bytes

²R²+²Æ²O

This answer is non-competing, since it uses features that have been implemented after the challenge was posted. Try it online!

This approach does not use floating point math, so it will give the correct answer as long as the intervening lists can fit into memory.

Idea

If (a, b, c) is a Pythagorean triple, there are strictly positive integers k, m, n such that the set equality {a, b} = {km2 - kn2, 2kmn} holds.

In particular, this means that a < b2 and b < a2, so for input a we can simply check if a2 + b2 is a perfect square for each b in {1, … a2}.

Code

            Input: x

²           Compute x².
 R          Get get range 1 ... x².
  ²         Square each integer in that range.
   +²       Add x² to each resulting square.
     Ʋ     Check if the resulting sums are perfect squares.
       O    Get all indices of ones.

Julia, 35 bytes

n->filter(i->hypot(i,n)%1==0,1:n^2)

This is an anonymous function that accepts an integer and returns an array.

For each i from 1 to the input squared, we compute the hypotenuse using Julia's built-in hypot function, and determine whether the fractional portion is 0. If so, we keep it, otherwise it's excluded.


CJam, 17 bytes

{:A2#{Amh1%!},1>}

This is an anonymous function that pops an integer from the stack and leaves an array in return.

Try it online!

Idea

If (a, b, c) is a Pythagorean triple, there are strictly positive integers k, m, n such that the set equality {a, b} = {km2 - kn2, 2kmn} holds.

In particular, this means that a < b2 and b < a2, so for input a we can simply check if a2 + b2 is a perfect square for each b in {1, … a2}.

Code

:A               Save the input in A.
  2#             Square it.
    {      },    Filter; for each B in {0, ..., A**2}:
     Amh           Calculate the hypotenuse of (A, B).
        1%!        Apply logical NOT to its fractional part.
                 Keep B if ! pushed 1.
             1>  Discard the first kept B (0).  

Tags:

Code Golf