Javed Akram
Javed Akram

Reputation: 15344

perfect side combination of right triangle

I want to get a list of: Sides of Right Triangle

which are perfectly whole numbers.(where each sides less than 100)

Example:

//I want these combination to be printed 
3, 4, 5
6, 8, 10                    |'.
5, 12, 13               12  |  '.    13   (Figure is just Example)
.                           |    '.
.                           |______'.
.                               5


// I don't want these
1, 1, 1.414....            |'.
.                        1 |  '.    √ˉ2  = 1.414.... (Figure is just Example)
.                          |    '.
                           |______'.
                               1

Update:

I do like this: But this is very heavy code(regarding optimization)

for(int i=1;i<100;i++)
{
     for(int j=1;j<100;j++)
     {
         for(int k=1;k<100;k++)
         {
           if(i*i + j*j == k*k)
           { 
                //print i, j, k
           } 
         }         

     }

}

Upvotes: 0

Views: 1983

Answers (6)

Dr. belisarius
Dr. belisarius

Reputation: 61016

In a declarative language (Mathematica):

FindInstance[x^2 + y^2==z^2 &&1<=z<=100 && 1<=y<=x<=100, {x, y, z}, Integers,100]

Upvotes: 0

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234354

You can improve your code by removing the innermost loop if you take advantage of the fact that for each pair of catheti, there is only one possible value for the hypotenuse. Instead of looping around to find that value, you can compute it using the Pythagorean theorem and test if it is an whole number.

Something like:

// compute the hypotenuse
var hypotenuse = Math.Sqrt(i*i + j*j);
// test if the hypotenuse is a whole number < 100
if(hypotenuse < 100 && hypotenuse == (int)hypotenuse)
{
     // here's one!
}

Some other improvements you can do include:

  • Once you've checked a pair of catheti (x,y), don't check for (y,x) again;
  • Once you find a triangle (x,y,z), you can include all triangles with the same sides multiplied by a constant factor (k*x, k*y, k*z), i.e, if you find (3,4,5) you can include (6,8,10), (9,12,15), (12,16,20), etc (this one might be a too much effort for little gains);

Upvotes: 1

Jon Hanna
Jon Hanna

Reputation: 113222

// Obvious min is 1, obvious max is 99.
for(int i = 1; i != 100; ++i)
{
  // There's no point going beyond the lowest number that gives an answer higher than 100
  int max = 100 * 100 - i * i;
  // There's no point starting lower than our current first side, or we'll repeat results we already found.
  for(int j = i; j * j <= max; ++j)
  {
    // Find the square of the hypotenuse
    int sqr = i * i + j * j;
    // We could have a double and do hyp == Math.Round(hyp), but lets avoid rounding error-based false positives.
    int hyp = (int)Math.Sqrt(sqr);
    if(hyp * hyp == sqr)
    {
      Console.WriteLine(i + ", " + j + ", " + hyp);
      // If we want to e.g. have not just "3, 4, 5" but also "4, 3, 5", then
      // we can also here do
      // Console.WriteLine(j + ", " + i + ", " + hyp);
    }
  }
}

Upvotes: 3

zebediah49
zebediah49

Reputation: 7611

A fairly good exhaustive search:

for(i=1;i<100;i++) {
    k=i;
    for(j=1;k<100;j++) {
        while(i*i+j*j<k*k) {
            k++;
        }
        if(i*i+j*j==k*k) {
            printf("%d %d %d", i, j, k);
        }
    }
}

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

I've used this formula in C# for generating Pythagorean triples in the past. But there are many other options on that page.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363467

What you're looking for are the Pythagorean triples.

Upvotes: 6

Related Questions