Pythagorean triplets

i made this program to print pythagorean triplets.
it prints each triplet twice how do i modify it so that it only prints a triplet once
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <math.h>

using namespace std;
bool persquare(double x)
{
    double y = sqrt(x) ;
    int z = y ;
    if(z==y)
    {
    return true ;}
 return false ;
}
int main()
{
    for(int i = 1; i < 10 ;i++)
    {
        for (int j = 1 ;j<10 ;j++)
        {
            double k = pow(i,2)+ pow(j,2) ;
            if(persquare(k)&&k<=100)
            {cout <<i<<" , "<<j<<" , "<<sqrt(k)<< endl;}

        }
    }

}
Last edited on
int j = i+1
ok ... that was easy thanks
All possible pythagorean integer triplets x, y, z are given by the formulas:

x = k*(m*m - n*n),
y = k*(2*m*n),
z = k*(m*m + n*n)


where m > n > 0 and k are integer numbers. It's easy to verify that x*x + y*y = z*z for any selection of m > n > 0 and k integer numbers. So, the appropriate code to generate the triplets (x, y, z) is...a piece of cake.


Last edited on
in my above code if i change the type of k in line 20 from double to integer(also changing the function defination) then the program prints many wrong values along with the right ones..
the value of k is anyway a integer so how does changing the type matters??
Last edited on
Help please ?
Try to change the function definition and the line 20 as follows:

bool persquare(int x)

int k = i * i + j * j;

In my archlinux box this gave the following output:

[condor@archie ~]$ ./wsx
3 , 4 , 5
6 , 8 , 10
[condor@archie ~]$
Last edited on
yup that does the trick but whats the problem with the pow() function??
> if i change the type of k in line 20 from double to integer

The floating point value (say 7.3) is narrowed to an int (7).


Avoid floating point for this; use integer operations.

A floating point value is an approximate representation a real number.
A value of an integral types is an exact representation of an integer (within a range).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    for( int i = 1; i < 100 ; ++i )
    {
        for( int j = i+1 ; j < 100 ; ++j )
        {
            int s = i*i + j*j ;
            int k = j+1 ;
            while( k*k < s ) ++k ;
            if( k*k == s ) std::cout << i << ',' << j << ',' << k << '\n' ;
        }
    }
}
i didnt understand why type of k is changed in line 20 ??
is i change the type of k in my code then it prints wrong values why is that ??
while using double type it gives the right values ??
quite confusing
The Pythagorean triples by their nature are integer values, there shouldn't be any need to use type double unless the numbers are very large, in which case type long long might be better. But int should be adequate here.

If your code prints the wrong values, then please show the code which is giving the problem.
heres the code

#include <iostream>
#include <math.h>

using namespace std;
bool persquare(int x)
{
double y = sqrt(x) ;
int z = y ;
if(z==y)
{
return true ;}
return false ;
}
int main()
{
for(int i = 1; i < 10 ;i++)
{
for (int j = 1 ;j<10 ;j++)
{
int k = pow(i,2)+ pow(j,2) ;
if(persquare(k)&&k<=100)
{cout <<i<<" , "<<j<<" , "<<sqrt(k)<< endl;}

}
}

}
heres the code
And what (presumably incorrect) results did you get?

I took a look at this with a couple of different compilers, one of them given certain values seemed to have a small inaccuracy in the pow() function, which when truncating the result to an integer gave an incorrect value.

But without seeing the results, I don't know whether that's the issue here or not.
Topic archived. No new replies allowed.