LoginSignup
3
1

More than 5 years have passed since last update.

会社で少し盛り上がった Project Euler をやってみる 009

Posted at

Problem 009

ピタゴラス数(ピタゴラスの定理を満たす自然数)とは a < b < c で以下の式を満たす数の組である.

a^2 + b^2 = c^2

例えば、 $ 3^2 + 4^2 = 9 + 16 = 25 = 5^2 $ である.
$ a + b + c = 1000 $ となるピタゴラスの三つ組が一つだけ存在する.
これらの積 abc を計算しなさい.


Answer 009 (Python)

import sys


class Problem9:

    def main(self, total):
        for a in range(1, total - 1):
            for b in range(a + 1, int((total - a) / 2) + 1):
                c = total - a - b
                if c ** 2 == a ** 2 + b ** 2:
                    return a * b * c


if __name__ == '__main__':
    total = 1000
    if len(sys.argv) == 2:
        total = int(sys.argv[1])
    p = Problem9()
    print(p.main(total))

(参考) Project Euler とは

Project Euler はプログラミングで数学の問題を解くサイトです。問題は600問以上あるので、勉強に使うのも良し、楽しむのも良しです。

サイトに登録すれば、解答を submit することができ、その場であっているか判定してくれます。

また、問題だけであれば、日本語に翻訳された Wiki もあるのでそちらを見ながらやると捗ると思います。

Project Euler(日本語訳)

3
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1