9 Special Pythagorean Triplet

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

a2+b2=c2a^2 + b^2 = c^2 For example, 32+42=9+16=25=52.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.

#include <iostream>
#include <limits>
#include <vector>
#include <math.h>

using namespace std;

void pause() { cin.ignore(numeric_limits<streamsize>::max(), '\n'); }

void practice1LoopTesting()
{
    for (int i = 0; i < 5; i++)
    {
        cout << i << ' ';
    }
}

void practice2LoopTesting()
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            cout << i << j << ' ';
        }
    }
}

void practice3LoopTesting()
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            for (int z = 0; z < 5; z++)
            {
                cout << i << j << z << ' ';
            }
        }
    }
}


int main()
{
    int a = 0;
    int b = 0;
    int c = 0;

    int sumABC = 1000;

    for (int a = 0; a < sumABC; a++)
    {
        for (int b = 0; b < sumABC; b++)
        {
            for (int c = 0; c < sumABC; c++)
            {
                // check 1
                if (a < b && b < c)
                {
                    // check 2
                    if (a*a + b*b == c*c)
                    {
                        // check 3
                        if (a + b + c == sumABC)
                        {
                            cout << a << ' ' << b << ' ' << c << ' ' << endl;
                        }
                    }

                }
            }
        }
    }

    cout << "done";



    // Practice with Combinations

    /*cout << "Single for loop: " << endl;
    practice1LoopTesting();
    cout << "\n\n";

    cout << "Double for loop: " << endl;
    practice2LoopTesting();
    cout << "\n\n";

    cout << "Triple for loop: " << endl;
    practice3LoopTesting();
    cout << "\n\n";*/

    pause();
}

Solution: 31875000

Last updated