All challenges
Medium 0 solved 0% acceptance

Right-Triangle Product

Find the largest side product of an integer right triangle with a given perimeter.

Problem

Find positive integers a < b < c with a² + b² = c² and a + b + c = perimeter. Return the largest product a × b × c among qualifying triangles, or −1 if there are none. This generalizes the fixed-perimeter source task and explicitly handles multiple answers.

Implement pythagorean_product(perimeter).

Input

perimeter: The required sum of the three sides.

Output

Return the largest product, or −1 when no triangle qualifies.

Constraints

3 ≤ perimeter ≤ 100,000. Products fit signed 64-bit integers.

Examples

Example 1
Input:
perimeter = 30

Expected output:
expected = 780
Input:
long long perimeter = 30;

Expected output:
long long expected = 780;
Input:
long perimeter = 30;

Expected output:
long expected = 780;

Explanation

The triangle is (5, 12, 13): 25 + 144 = 169 and 5 + 12 + 13 = 30. Its product is 780.

Example 2
Input:
perimeter = 11

Expected output:
expected = -1
Input:
long long perimeter = 11;

Expected output:
long long expected = -1;
Input:
long perimeter = 11;

Expected output:
long expected = -1;

Explanation

No increasing positive integer right-triangle sides sum to 11.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.