Tutte le sfide
Media 1 risolta 100.0% di accettazione

Right-Triangle Product

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

Cosa imparerai

  • Search integer right triangles by perimeter
  • Reduce three loops to two
  • Check the Pythagorean condition

Problema

Mostra traduzione

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.

Vincoli

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

Esempi

Esempio 1
Input:
perimeter = 30

Output atteso:
expected = 780
Input:
long long perimeter = 30;

Output atteso:
long long expected = 780;
Input:
long perimeter = 30;

Output atteso:
long expected = 780;

Spiegazione

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

Esempio 2
Input:
perimeter = 11

Output atteso:
expected = -1
Input:
long long perimeter = 11;

Output atteso:
long long expected = -1;
Input:
long perimeter = 11;

Output atteso:
long expected = -1;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Only certain perimeters can form a right triangle, so first narrow the search using the side-sum constraint.

Suggerimento 2

For each possible shortest side, derive the other two sides from the perimeter and check the right-triangle condition.

Suggerimento 3

More than one triangle may fit; keep the largest product and return no-solution when nothing valid appears.

Complessità attesa

O(perimeter^2) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 ННикита Python 957 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.