Poblems in pythagorean triples

Hi, I'm trying to write a code that produce all the first 100 pythagorean triples but by the time I'm only capable of obtain the first one (3, 4, 5) and I don't understand why. Please, help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

int main ()
{
	for (int x = 0, y = 1, z = 2; x <= 100; x++, y++, z++)
	{
		if (((x*x) + (y*y)) == z*z)
		{
			cout << x << y << z << endl;
		}
	}
	system("pause");
	return 0;
}
You should use a double for loop.
Below is a corrected code.

#include<iostream>
using namespace std;

int main ()
{
for (int x = 0; x <= 100; x++)
{
for (int y = 1, z = 2; y <= 100; y++, z++)
{
if (((x*x) + (y*y)) == z*z)
{
cout << x << y << z << endl;
}
}
}
system("pause");
return 0;
}
Ok, thank you very much.
/*This is the correct solution.*/

#include<iostream>
using namespace std;

int main ()
{
for (int x = 0; x <= 100; x++)
{
for (int y = 1; y <= 100; y++)
{
for (int z = 2; z <= 100; z++)
{
if (((x*x) + (y*y)) == z*z)
{
cout << x << ", " << y << ", " << z << endl;
}
}


}
}
system("pause");
return 0;
}
/*This is the correct solution.*/

Not exactly. Have you looked at the first hundred or so triples you generate?

A possible solution? It has duplicated triples, though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>

int main ()
{
  for (int i = 0; i <= 100; ++i) {
    for (int j = 0; j <= 100; ++j) {
      for (int k = 0; k <= 100; ++k) {
        if (pow(k, 2) == pow(i, 2) + pow(j, 2)) {
          std::cout << "("
                    << i << (", ")
                    << j << (", ")
                    << k
                    << (")") << std::endl;
        }
      }
    }
  }

  return 0;
}
Last edited on
elaleph wrote:
A possible solution?

Also generates incorrect triples.

http://ideone.com/OrFZZy
Last edited on
And this?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>

int main ()
{
  double h;
  for (int a = 1; a <= 100; ++a) {
    for (int b = a; b <= 100; ++b) {
      h = sqrt(pow(a, 2) + pow(b, 2));
      if (h == int(h) && h <= 100) {
        std::cout << "("
                  << a << (", ")
                  << b << (", ")
                  << h
                  << (")") << std::endl;
      }
    }
  }

  return 0;
}
Last edited on
Or this?

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

int main ()
{
  double h;
  int limit;
  int posibilities = 0;
  int triples = 0;

  for (int a = 1; a <= 100; ++a) {
    limit = sqrt(pow(100, 2) - pow(a, 2));
    for (int b = a; b <= limit; ++b) {
      posibilities++;
      h = sqrt(pow(a, 2) + pow(b, 2));
      if (h == int(h)) {
          std::cout << "("
                  << a << (", ")
                  << b << (", ")
                  << h
                  << (")") << std::endl;
				triples++;
      }
    }
  }
  
  std::cout << posibilities << " posibilities" << std::endl;
  std::cout << triples << " triples" << std::endl;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.