Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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 a*b*c.
- */
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- #define LIMIT 1000
- using namespace std;
- // Auxiliary Functions
- bool isPythagorianTriplete(int a, int b, int c){
- if(c == sqrt(a*a + b*b)){
- return true;
- }
- return false;
- }
- int main()
- {
- int a, b, c;
- for(int i=0; i<LIMIT; i++){
- for(int j=0; j<LIMIT; j++){
- for(int k=0; k<LIMIT; k++){
- if(isPythagorianTriplete(i, j, k) == true && i < j && j < k && i+j+k == 1000){
- a = i;
- b = j;
- c = k;
- }
- }
- }
- }
- cout << "These numbers are: " << a << ", " << b << " and " << c << ", so " << a << " * " << b << " * " << c << " = " << a * b * c << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement