Skip to the content

The twelve-knot rope

Problem

Four thousand years ago, Egyptian surveyors laid out square corners with a knotted rope. With 12 equal segments they formed a triangle with sides 3, 4 and 5, and the angle between the 3 and the 4 came out right.

Write a function that takes p, an integer greater than 0: the rope's segments. Return a list [a, b, c] of integers greater than 0 with a < b < c, a + b + c == p and a*a + b*b == c*c. If there are several, the one with the smallest a. If there are none, return the empty list [].

With 12 the answer is [3, 4, 5]: they add up to 12, and 9 + 16 = 25. With 60 there are two, [10, 24, 26] and [15, 20, 25]; [10, 24, 26] wins because it starts at 10. With 7 there are none and you return [].

Examples

  • The twelve-knot rope

    12 → [3, 4, 5]

  • Thirty segments

    30 → [5, 12, 13]

  • Twelve, doubled

    24 → [6, 8, 10]

  • Seven is not enough

    7 → []

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def right_triangle(p):
    pass

JavaScript

function rightTriangle(p) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →