LoginSignup
2
2

More than 5 years have passed since last update.

Python で Project Euler #9「特別なピタゴラス数」

Last updated at Posted at 2014-11-17

Problem 9 「特別なピタゴラス数」

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

a^2 + b^2 = c^2

例えば,

3^2 + 4^2 = 9 + 16 = 25 = 5^2

である.
a + b + c = 1000 となるピタゴラスの三つ組が一つだけ存在する.
これらの積 abc を計算しなさい.

Python
n = 1000

seq = range(1, n+1)

def is_pythagorean(a, b, c):
  return a**2 + b**2 == c**2

pythagorean = None
for a in seq:
  if(pythagorean): break
  for b in range(a+1, n+1):
    c = n - b - a
    if(c > b and is_pythagorean(a, b, c)):
      pythagorean = (a, b, c)
      break

result = reduce(lambda x,y: x*y, pythagorean)

print result
print result == 31875000
print pythagorean
結果
31875000
True
(200, 375, 425)
2
2
0

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
2
2