Pythagorean Triples Program

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?

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace 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();
}
What is a "Pythagorean Triple"?
Its hard to explain, heres a link though. https://www.mathsisfun.com/pythagorean_triples.html
closed account (j8pEwA7f)
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.
Last edited on
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).
closed account (j8pEwA7f)
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)?
This is the updated code btw.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace 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.
Last edited on
What do the function one(), two() and three() do?
"Pythagorean Triple" is actually a very simple concept.
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.
Last edited on
No need for sqrt() or floor().

1
2
3
4
bool is_pythag_triplet( const int a, const int b, const int c )
{
    return a*a + b*b == c*c;
}
Last edited on
So I am making a program to calculate as many Pythagorean Triples as I can
- from OP's headline post

Try this to calculate all P triples upto and including the given number:

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
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<cmath>
#include<vector>
#include<tuple>

void findPythogorean(int P);
using namespace 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
Last edited on
Dickson's method: https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples#Dickson.27s_method

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>

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

        const int 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

                static int cnt = 0 ;
                std::cout << ++cnt << ". (" << r+s << ',' << r+t << ',' << r+s+t << ")\n" ;
            }
        }
    }
}

http://coliru.stacked-crooked.com/a/e412f909f2f3daee
Topic archived. No new replies allowed.