/* * bhatt.umang7@gmil.com */ /* * 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. */ #include<iostream> #include<math.h> using namespace std; bool isPerfectSquare(int no ) { bool ans = true ; for(int i = 2 ; i < no ; i++) { if ( no % i == 0 ) { ans = false; break; } } return ans; } int main() { int ans ; for (int i = 1 ; i <= 1000 ; i++ ) { for (int j = 1 ; j <= 1000 ; j++ ) { for (int k = 1 ; k <= 1000 ; k++ ) { if (((i*i) + (j*j)) == (k*k) ) { if( i < j < k) { if(i+j+k==1000) { ans = i * j * k ; goto show_ans; } } } } } } show_ans: cout << ans << "\n"; }
Thursday, January 19, 2012
Pythagoras
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment