Project Euler Homework
Functions

009.cpp File Reference

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

#include <iostream>

Go to the source code of this file.

Functions

int main ()

Detailed Description

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, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000.Find the product abc.

Definition in file 009.cpp.


Function Documentation

int main ( )

All possible solution of Pythagorean triplet are: $a=2mn$, $b=m^2+n^2 $, and $c=m^2+n^2 $for all positive integer m, n where m>n. Therefore

\[a+b+c=(2mn)+(m^2-n^2)+(m^2+n^2)=2(m^2+2mn)=1000\Rightarrow m^2+mn=500 \]

Upper limit of m:

\[m^2+mn<=m^2+m^2<=1000 \Rightarrow m <= 22 \]

There are 21 possible values of m, so $21\times(21+1)/2=231 $ possible solutions of $(m,n)$.

Since $m^2=500-mn $ and $m^2\equiv 0 \textrm{or} 1 (\textrm{mod} 3)$, $mn=1 \textrm{ or } 2 (\textrm{mod} 3)$. i.e. m,n are not multiple of 3, so $n = (500-m^2)/m = 500/m - m $and m is a factor of 500.

Definition at line 26 of file 009.cpp.

 All Classes Files Functions Variables