-
Help Newbie
#include<iostream.h>
//I ONLY NEED THE NUMBERS THAT PROVE TRUE
//IN COMPUTING PYTHAGOREAN THEORM
//X^2 + Y^2 = Z^2
main()
{
int z,x,y;
for (x = 0 ; x <=500;x++)
for ( y = 0; y <=500; y++)
for (z = 0; z <=500; z++)
//I only need the z that proves pythagorean thereom printed
//HELP ME
if ((z*z) == (x*x) + (y*y))
cout<<x<<" + "<<y<<" = "<<z<<"\n";
return 0;
}
-
The Program is not running properly..its suppossed to output only the possibilities of Z being true which is when x^2 and y^2 are equal to the square of Z...
Copy and paste the program and run it...
the output is not correct
please help me..
-
The program was correct. The output is what confused you ;)
Code:
int main()
{
int z,x,y;
for (x = 0; x <=500;x++)
for ( y = 0; y <=500; y++)
for (z = 0; z <=500; z++)
if(z != 0 && x != 0 && y != 0)
if ((z*z) == ((x*x) + (y*y)) )
cout<<x<<"^2 + "<<y<<"^2 = "<<z<<"^2 ...because: " << x * x <<" + "<< y * y <<" = "<< z * z <<"\n";
getch();
getch();
return 0;
}
-
And maybe you don't need three for statements.. only some math :)
the numbers
z=p^2 + q^2
x = p^2 - q^2
y = 2*p*q
are pythagoric for two positive integers p,q (p > q).
(you can prove it yourself using (a+b)^2 = a^2 + 2*a*b + b^2).
As far as I know all pythagoric numbers can be written using some p,q but I haven't proved that yet...