Creating Unique Pythagorean Triples in VS2013

Hi, I'm rather new to programming, and while I get a lot of the concepts, I have trouble when trying to implement math concepts I don't understand into the programming.

In this case I need to find Unique Pythagorean Triples up to a user entered limit. Using this user entered limit I need to find all triples > 0 up to the limit. Then print them into three rows. I must use an outer do while loop.

sample image of what it should look like: http://i.imgur.com/ycg9YlD.png

Specific Instructions: http://i.imgur.com/xAB5GUo.png

What I don't understand how to do is to is actually create the a, b and lower-than-limit c values. Would I use a loop to slowly decrease the value of the limit and print in reverse? I understand the a^2 + b^2 = c^2, but how do I create an algorithm that will check if that this is true, and increment the values individually to keep checking?

Sorry if its a confusing question, but I'm confused!

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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main () {

int n = 0;
int sides = 0;
int a = 1;
int b = 1;
int c = 2;
int limit = 0;

cout << "Enter the upper limit for the sides of a right triangle: ";
cin >> limit;
cout << "Side 1  Side 2  Side 3" << endl;



do {
	for (n=1; n <= limit; ++n){
	if ((a * a) + (b * b) == (c * c)){
		cout << a <<"    " << b << "    "<< c;
	}
	}
		if ((a * a) + (b * b) != (c * c)) {
		++a;
		++b;	
	}
	
	} while ( n <= limit);


		return 0;
}
Seems you're going to need nested loops, and it seems you only have to worry about integers. Let's say your shorter side is of length a, and your longer side is of length (a + x), so... a^2 + (a+x)^2 = limit^2.

Your first for loop would increase the value of a, and your nested for-loop would increase the value of x. A would start at 1 and x would start at 0.

1
2
for (int a = 1; 2 * pow(a, 2) <= pow(limit, 2); a++)
    for (int x = 0; pow(a, 2) + pow(a+x, 2) <= pow(limit, 2); x++)


Any of that making sense? It can be a very convoluted answer, but I believe its mathematically sound.

[Edited code after gaining a better understanding of the problem. The solution of nested for-loops still stands.
Last edited on
Hm. On second thought, you'd need three nested for-loops. The first would be the one you provided in your original post and the two I provided.

1
2
3
4
for (int c = 1; c <= limit; c++)
    for (int a = 1; pow(a, 2) + pow(a, 2) <= pow(c, 2); a++)
        for (int x = 0; pow(a, 2) + pow(a+x, 2) <= pow(c, 2); x++)
            //check to see if if a^2 + (a+x)^2 equals c^2 
First off thank you for quick help! I can't believe I didn't find this forum sooner, as it appears to be a great resource for me!

I think I kind of get it, in the sense that since its a^2 + b^2 that a+/- some value will equal b and that will equal c^2.

However, when the loop runs, I got a constant stream of changing numbers for a, and a consistent 1 & 2 for b & c.

Questions I have: why am I multiplying pow(a, 2) by 2 in the first loop? that would give me 2a^2 wouldn't it?
What the first loop is basically saying is:

a = 1, when 2*a^2 is less than or equal to limit^2; increase a

where the second loop reads as:

x = 0, when a^2 +(a+x)^2 are less than or equal to limit^2, increase x.

So this will increment the values at different times in the loop.

So then why is it that I get this stream of: |random number||1||2|
for my cout values? Also how would I make it stop, do I need a break statement or should it stop automatically once it tries to surpass the limit set in the loop?

I guess I'm less adept than I thought :/


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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {

int x = 0;
int sides = 0;
int a = 1;
	int b = 1;
int c = 2;
	int limit = 0;

cout << "Enter the upper limit for the sides of a right triangle: ";
cin >> limit;
	cout << "Side 1  Side 2  Side 3" << endl;



	do {
		for (int a = 1; 2 * pow(a, 2) <= pow(limit, 2); a++){
			for (int x = 0; pow(a, 2) + pow(a + x, 2) <= pow(limit, 2); x++){
				cout << a << "    " << b << "    " << c << endl;
				
			}

		}
	} while (c <= limit);

return 0;
} 


Last edited on
Also, you never actually have a condition to check if a^2 + (a+x)^2 = c^2 and you also never change the value of b or c, so it will always output those static numbers.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int upper;
	int count;
	char choice;
	do
	{
		cout<<"\nEnter upper limit for each side of the triangle: ";
		cin>>upper;
		count  = 0;
		for(int i = 0; i < upper; i++)
		{
			for(int j = i+1; j < upper; j++)
			{
				for(int k = j+1; k <= upper; k++)
				{
					if(i*i + j*j == k*k)
					{
						cout<<i<<setw(5)<<j<<setw(5)<<k<<endl;
						count++;
					}
				}
			}
		}
		cout<<"A total of "<<count<<" triplets were found"<<endl;

	        cout<<"Y or y to continue, anything else to quit: "<<endl;
	        cin>>choice;
	}while(choice == 'y' || choice == 'Y');
	
	system("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.