Zachary Stence

Problem 9: Special Pythagorean triplet

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.

I first solved this problem May 22nd, 2015 in the summer after my junior year of highschool. I do not have the original var I wrote, but I have solved the problem again and included the code below.

I solved this problem with a simple brute force approach of looping through all possible numbers a, b, c and printing the product abc if it satisfies a2 + b2 = c2 and a + b + c = 1000.

#include <iostream>

int main() {
  for (int c = 0; c < 1000; c++) {
    for (int b = 0; b < c; b++) {
      for (int a = 0; a < b; a++) {
        if (a*a + b*b == c*c && a + b + c == 1000)
          std::cout << a*b*c << std::endl;
      }
    }
  }
}

Answer: 31875000