How can I make this program tell me all the pythagorean triples with no repeats?

I'm stuck on how I should make this program loop correctly to make it show all the triples of the pythagorean theorem that make perfect triangles. My instructor told me that my logic was correct but I know that my syntax is wrong. What should I change so that it shows all the triples up to 500 and display the count of them all?

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
  #include <cstdlib>
#include <iostream>

using namespace std;

int pythagorean(int a, int b, int c)
int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    cout << pythagorean << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean (int a, int b, int c){
    int total;
    for (int a=1;((a>=1)&&(a<500)); a++)
    for (int b=1;((b>=1)&&(b<500)); b++)
    for (int c=1;((c>=1)&&(c<500)); c++)
    if ((a*a) + (b * b)) = c * c)
      if ((a<b)&&(a<c)){
       cout << a << " " << b << " " << c << endl;
       total = total + 1;
    }
}
Could you give us a prompt, please?
Here is the prompt:

When three integers a, b, and c satisfy the pythagorean equation, such as 3, 4, 5 do, the result is called a Pythagorean Triple. Create a program to list all of the Pythagorean Triples with integers of 500 or less. Also, give the total number of unique Pythagorean triples found by your program.

Try this out:

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

using namespace std;

int main()
{
    for (int a = 1; a <= 500; ++a)
        for (int b = a; b <= 500; ++b)
            for (int c = b; c <= 500; ++c)
                if (a * a + b * b == c * c)
                    cout << a << ' ' << b << ' ' << c << endl;

    return 0;
}
I need to use a function in it and I fixed a few things which allowed it to run. However, when I run it now, the program repeats forever without stopping or using numbers that satisfy the pythagorean theorem. Here is what I have now:
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
#include <cstdlib>
#include <iostream>

using namespace std;

int pythagorean();
int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    int count = pythagorean();
    cout << count << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean (){
    int total = 0;
    for (int a=1; a<=500; a++){
    for (int b=1; b<=500; b++){
    for (int c=1; c<=500; c++){
    if (a*a) + (b * b) == (c * c);{
      if ((a<b)&&(a<c)){
       cout << a << " " << b << " " << c << endl;
       total = total + 1;
    }
}
}
}
}
}
I'm not giving you all the answers, but I'm going to comment where all the obvious problems are.

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 <cstdlib>
#include <iostream>

using namespace std;

int pythagorean();

int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    int count = pythagorean();
    cout << count << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean ()
{
    int total = 0;
    for (int a=1; a<=500; a++)
	{
		for (int b=1; b<=500; b++)  //if you change so that b is a + 1, you can get rid of an if statement.  Do you see it?
		{
			for (int c=1; c<=500; c++)  //this loop and the if following aren't completely necessary.  You can use a and b to find c
			{
				if (a*a) + (b * b) == (c * c);  //semicolon?  Parentheses around the if?
				{
					if ((a<b)&&(a<c))  //a and b will always be less than c
					{
						cout << a << " " << b << " " << c << endl;
						total = total + 1;
					}
				}
			}
		}
	}
  //return statement?
}
Last edited on
Alright, I tried to add your suggestions but am confused about what the return statement should be...
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 <cstdlib>
#include <iostream>

using namespace std;

int pythagorean();
int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    int count = pythagorean();
    cout << count << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean (){
    int total = 0;
    for (int a=1; a<=500; a++)
        {
             for (int b= a + 1; b<=500; b++)
                 {
                      for (int c=1; c<=500; c++)
                          {
                               if ((a*a) + (b * b) == (c * c))
                               {
       cout << a << " " << b << " " << c << endl;
       total = total + 1;
    }
                           }
                  }
          }
    }
Wait, I tried to change some other stuff per Josue Molina's suggestion and now my program runs and hits a stop, but it doesn't have a count of how many there are and the first triple begins at 50, which I know is incorrect.
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
int pythagorean();
int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    int count = pythagorean();
    cout << count << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean (){
    int total = 0;
    for (int a=1; a<=500; a++)
        {
             for (int b=a; b<=500; b++)
                 {
                      for (int c=b; c<=500; c++)
                          {
                               if ((a*a) + (b * b) == (c * c))
                               {
       cout << a << " " << b << " " << c << endl;
       total = total + 1;
    }
                           }
                  }
          }
          return 0;
    }
Check these out.
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
int pythagorean();
int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    int count = pythagorean();
    cout << count << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean (){
    int total = 0;
    for (int a=1; a<=500; a++)
        {
             for (int b=a; b<=500; b++)  //you should start b at a + 1
                 {
                      for (int c=b; c<=500; c++)  //yet again you should start at b + 1
                          {
                               if ((a*a) + (b * b) == (c * c))
                               {
       cout << a << " " << b << " " << c << endl;  //INDENTATIONS
       total = total + 1;  //INDENTATIONS
    }  //INDENTATIONS
                           }
                  }
          }
          return 0;  //what is the return statement supposed to do?
    }


You NEED indentations to make your code readable.

And your pythagorean triplets don't start at 50. The console can't display every line output.
Last edited on
I think I got it... I was just overthinking a lot of things but I understand now. Thank you!
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
#include <cstdlib>
#include <iostream>

using namespace std;

int pythagorean();
int main(int argc, char *argv[])
{
    cout << "Pythagorean triples: " << endl;
    int count = pythagorean();
    cout << "There are " << count << " pythagorean triples" << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int pythagorean (){
    int total = 0;
    for (int a=1; a<=500; a++)
        {
             for (int b= a + 1; b<=500; b++)
                 {
                      for (int c= b + 1; c<=500; c++)
                          {
                               if ((a*a) + (b * b) == (c * c))
                               {
                                  cout << a << " " << b << " " << c << " , ";
                                  total = total + 1;
                                  }
                           }
                  }
          }
          return total;
)
Topic archived. No new replies allowed.