Im trying to learn c++ and complete a learning task:
This is the task:
A Pythagorean triplet is a set of three integers, a, b, and c, for which it holds true that a^2 + b^2 = c^2. Write a function that takes an std::vector as input and returns an std::vector where all the Pythagorean triplets are removed. For example, given an input consisting of {6, 25, 5, 3, 7, 24, 4, 23}, the function should return {6, 23} because 3^2 + 4^2 = 5^2 and 7^2 + 24^2 = 25^2 are Pythagorean triplets.
Im having trouble getting the output {6 ,23}
This is my code so far.
#include <iostream>
#include <vector>
#include <cmath>
bool isPythagorasTriplet(int a, int b, int c)
{
if (pow(a, 2) + pow(b, 2) == pow(c, 2)){
return true;
} else return false;
}
std::vector<int> pythagorasTriplet(std::vector<int> input)
{
std::vector<int> notTriplets;
bool isTriplets = false;
for (int i = 0; i < input.size(); i++){
isTriplets = false;
for (int j = 0; j < input.size(); j++){
for (int k = 0; k < input.size(); k++){
if (i == j || i == k || j == k){
continue;}
if (isPythagorasTriplet(input[i], input[j], input[k])){
isTriplets = true;
// notTriplets.push_back(input[i]);
// notTriplets.push_back(input[j]);
// notTriplets.push_back(input[k]);
}
}
}
if (isTriplets == false) notTriplets.push_back(input[i]);
}
return notTriplets;
}
int main()
{
std::vector<int> input = {6, 25, 5, 3, 7, 24, 4, 23};
std::vector<int> noTriplets = pythagorasTriplet(input);
for (int i = 0; i < noTriplets.size(); i++){
std::cout << noTriplets[i] << std::endl;
}
return 0;
}