-4

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;
}
  • 1
    Any C or C++ program that uses `pow()` with two integer values is automatically broken by default. This is because, for example, `pow(10,2)` is not 100. It really isn't. This is not what `pow` is for. This is explained in every C++ textbook, which discusses why [floating point math is broken](https://stackoverflow.com/questions/588004/). Unfortunately, of all those web site that promise that solving their useless coding puzzles will turn anyone into an elite C++ uberhacker, none of them explain these C++ fundamentals, they're just a list of random coding puzzles. That's where the problem is. – Sam Varshavchik May 29 '23 at 20:48
  • Are you allowed to sort the original vector? – Bob__ May 29 '23 at 20:49
  • @SamVarshavchik would a * a + b* b = c * c be better? – user13170084 May 29 '23 at 20:50
  • I'd also follow the *"all the Pythagorean triplets are removed"* part quite literally. – Bob__ May 29 '23 at 20:56
  • @EricPostpischil better now? – user13170084 May 29 '23 at 20:59
  • "would a * a + b* b = c * c be better?" Almost certainly be faster (but compilers are getting smarter all the time) and it's guaranteed to be precise (so long as the computation remains in bounds). – user4581301 May 29 '23 at 21:09
  • 1
    Are you sure that the problem is well-posed? What do you get for the vector { 5, 12, 13, 3, 4 }, for example? { 3, 4 }, { 12, 13 } or { }? – lastchance May 30 '23 at 06:51

1 Answers1

0

Your code is supposed to find each number in the list that does not form a Pythagorean triple with any other numbers in the list. However, when evaluating whether or not to include input[i] in the list, it evaluates only whether input[i] forms a Pythagorean triple in the first position as passed to isPythagorusTriplet with two other numbers. It therefore misses cases where input[i] is the hypotenuse of a Pythagorean triple.

Changing the body of isPythagorusTriplet to return a*a + b*b == c*c || b*b + c*c == a*a || c*c + a*a == b*b; remedies this. (There are also optimizations that can be made to avoid redundant checks.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • you legend this little change made it so my output came to be {6,23} thanks a lot. And sorry to all for the bad explanation, but I appreciate your efforts – user13170084 May 29 '23 at 21:05