Skip to content

Commit

Permalink
Add a solution for ademclk#37
Browse files Browse the repository at this point in the history
  • Loading branch information
Legoeggolas committed Aug 28, 2022
1 parent e5f7897 commit 9075ef1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
38 changes: 38 additions & 0 deletions challenges/C++/pythagorean_triplet/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "solution.hpp"

std::array<int, 3> pythaTriplet() {
auto sq = [](int x) -> int {
return x * x;
};

std::array<int, 3> triplet = {0, 0, 0};
bool found = false;
// since a + b + c = 1000
// the max we can go is a = 332, b = 333, c = 334
for (int a = 1; a <= 332 && !found; a++) {
for (int b = a + 1; b <= 333 + (332 - a) && !found; b++) {
int c = 1000 - (a + b);

if (sq(a) + sq(b) == sq(c)) {
triplet = {a, b, c};
found = true;
}
}
}

return triplet;
}

/*
int main() {
std::cout << "The values are: ";
for (auto const &val : pythaTriplet()) {
std::cout << val << " ";
}
std::cout << std::endl;
// prints 200 375 425
return 0;
}
*/
9 changes: 9 additions & 0 deletions challenges/C++/pythagorean_triplet/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef PYTHA_TRIP_H
#define PYTHA_TRIP_H

#include <array>
//#include <iostream>

std::array<int, 3> pythaTriplet();

#endif

0 comments on commit 9075ef1

Please sign in to comment.