So I am making a program to calculate as many Pythagorean Triples as I can. However when I debug the program, even though I have "_getch()" to pause the console, it closes straight away with normal execution code 0. Any ideas?
#include <iostream>
#include <math.h>
#include <conio.h>
usingnamespace std;
int A, B, C;
int main()
{
A = 1;
B = 1;
C = 2;
int a();
}
void one()
{
double a;
a = sqrt((C*C) - (B*B));
if (a - floor(a) == 0)
{
if (A||B > C)
{
C = C + 1;
}
cout << A << " " << B << " " << C << endl;
A = A + 1;
}
else
{
void two();
}
}
void two()
{
double b;
b = sqrt((C*C) - (A*A));
if (b - floor(b) == 0)
{
if (A || B > C)
{
C = C + 1;
}
cout << A << " " << B << " " << C << endl;
B = B + 1;
}
else
{
void three();
}
}
void three()
{
double c;
c = sqrt((A*A) + (B*B));
if (c - floor(c) == 0)
{
cout << A << " " << B << " " << C << endl;
C = C + 1;
}
else
{
void one();
}
_getch();
int main();
}
Calling a function does not include the return type (just one() instead of void one()). void one() declares a function in that scope, while one() calls the function.
Note that you'll have to declare these functions (in global scope) if their implementation is after where they're called.
Thanks ralismark, i thought my code didnt look right. I fixed all the mistakes however now im getting a stack overflow error. Im using visual studio. This is the error:
Unhandled exception at 0x01121E59 in Pythagorean Triples.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00072FE4).
Stack overflows usually happen when you're recursively calling functions. It probably happens because you have no end condition (you're trying to calculate as many as you can).
Maybe have a counter for the number of triples found, and terminate when that is reached? Or change it to an iterative algorithm (I'm not sure how though)?
#include <iostream>
#include <math.h>
#include <conio.h>
usingnamespace std;
int A, B, C;
void one(), two(), three();
int main()
{
A = 1;
B = 1;
C = 2;
one();
}
void one()
{
while (1)
{
double a;
a = sqrt((C*C) - (B*B));
if (a - floor(a) == 0)
{
if (A || B > C)
{
C = C + 1;
}
cout << A << " " << B << " " << C << endl;
A = A + 1;
two();
break;
}
else
{
two();
break;
}
}
}
void two()
{
while (1)
{
double b;
b = sqrt((C*C) - (A*A));
if (b - floor(b) == 0)
{
if (A || B > C)
{
C = C + 1;
}
cout << A << " " << B << " " << C << endl;
B = B + 1;
three();
break;
}
else
{
three();
break;
}
}
}
void three()
{
while (1)
{
double c;
c = sqrt((A*A) + (B*B));
if (c - floor(c) == 0)
{
cout << A << " " << B << " " << C << endl;
C = C + 1;
main();
break;
}
else
{
one();
break;
}
}
}
Ok so i've added a counter which limits the amount of iterations to 500. Theres no more stack overflow. However im not getting any output. Any idea why? Ive also added a system("pause") to stop the program after the 500 iterations.
Haha i just realised that i was over complicating it. Essentially what i was trying to do was to increment a, b and c by one each to calculate all the combinations of numbers i could have, which could be Pythagorean triples. I thought that it would be better if i had separate functions to do this. However im changing the code completely now.
#include<iostream>
#include<cmath>
#include<vector>
#include<tuple>
void findPythogorean(int P);
usingnamespace std;
int main(){
findPythogorean(10000);//execution time 0.547s
}
void findPythogorean(int P){
vector<pair<int,int>>v;
int p = 1;
while (p<=P){
for (int i = 2; i <= sqrt(P)+1; i++){
for (int j = 1; j < i; j++){
if(sqrt((i * i) + (j * j) ) == sqrt(p)){
v.emplace_back(i,j);
}
}
}
p++;
}
if(v.empty()){
cout<<"There are no Pythogorean triangles for numbers upto and including "<<P<<"\n";
}
else {
cout<<"Pythogrean triangles upto and including :"<<P<<" are\n";
for(auto& itr : v){
int i = sqrt(pow(get<0>(itr), 2) + pow(get<1>(itr),2));
cout<<get<0>(itr)<<"\t"<<get<1>(itr)<<"\t"<<i<<"\n";
}
}
}
The code uses some C++ 11 features (range loops, vector pair declaration syntax etc). If anything is unclear please read it up and come back here if you have any further queries
PS: trained mathematicians, which I'm not, on this forum may be able to propose tighter limits and/or other conditions for the loops to speed up execution time
#include <iostream>
int main()
{
for( int r = 2 ; r < 501 ; r += 2 ) // for even positive integers r < 501
{
// to find integer solutions to x^2 + y^2 == z^2,
// find positive integers r, s, and t such that r^2 == 2.s.t
// ie. s.t == r^2/2 or s and t are integer factors of r^2/2
// then x == r+s, y == r+t, z == r+s+t
constint rs2 = r * r / 2 ; // r^2 / 2
int t = r ;
for( int s = 1 ; s < t ; ++s )
{
if( rs2%s == 0 ) // if s is an integer factor of r^2/2
{
t = rs2/s ; // s.t == r^2/2
// r+s, r+t, r+s+t form a pythagorean triplet
staticint cnt = 0 ;
std::cout << ++cnt << ". (" << r+s << ',' << r+t << ',' << r+s+t << ")\n" ;
}
}
}
}