中級 1件解決済み 正答率100.0%

Right-Triangle Product

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

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

例

例 1
入力:
perimeter = 30

期待される出力:
expected = 780
入力:
long long perimeter = 30;

期待される出力:
long long expected = 780;
入力:
long perimeter = 30;

期待される出力:
long expected = 780;

解説

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

例 2
入力:
perimeter = 11

期待される出力:
expected = -1
入力:
long long perimeter = 11;

期待される出力:
long long expected = -1;
入力:
long perimeter = 11;

期待される出力:
long expected = -1;

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 ННикита Python 957 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に1件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。