본문 바로가기

problem solving/Project Euler

Problem 9 - Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.

링크

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^(2) + b^(2) = c^(2)

For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2).

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.


python


python - more efficient

A Pythagorean triplet (a,b,c) has gcd(a,b) = gcd(b,c) = gcd(c,d), and is by definition primitive if gcd(a,b,c) = 1
and Pythagorean triplets can be represented as

a = m^2 - n^2, b = 2mn, c = m^2 + n^2
while 
m > n > 0

then Pythagorean triplet will be primitive iff exactly one of m, n is even and gcd(m, n)=1

Efficient algorithm use it