vendredi 25 août 2017

pow() function inside if in C++

the algorythm is supposed to find pythagorean triples a,b,c I'm a begginer so i just created three "for" loops and in the last loop when i check if a^2+b^2=c^2, however, when i use pow() inside if statement [if (pow(a,2)+pow(b,2)==pow(c,2)) it doesn't find small triples, like 3,4,5(although it checks those values), but it finds the big ones, for example 23 264 265. Everythings fine when i change "pow()" to the normal multiplication[aa+bb=c*c]. What's the problem with small triples?

#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int n,a,b,c;
int main()
{
    cout << "n:: " ;
    cin >> n;
    system("cls");
    for (int a=1 ; a<n; a++){
        for (int b=a+1; b<n; b++){
            for (int c=b+1; c<n; c++){
                    if (a*a+b*b==c*c) cout << a << " " << b << " " << c << endl;
            }
        }
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire