Pythagorean Triplets

4 minute read

Definition :

“Pythagorean triplets” are integer solutions to the Pythagorean Theorem (It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides).

image-center For a right triangle, the side is the hypotenuse, the side opposite to the right angle. The side is the shorter of the two sides adjacent to the right angle. Hence .

For example,

Concept and Theory

First Generating Formula

  • Every odd number is the side of a Pythagorean triplet.
  • The side of a Pythagorean triplet is simply .
  • The side is .

Here, and are always odd; is always even. These relationships hold because the difference between successive square numbers is successive odd numbers. Every odd number that is itself a square (and the square of every odd number is an odd number) thus makes for a Pythagorean triplet.

Euclid’s formula

Euclid’s formula is a fundamental formula for generating Pythagorean triples given an arbitrary pair of integers and with . The formula states that the integers

form a Pythagorean triplet.

Primitive Triplets : If and only if and are coprime and not both odd or not both be even. Basically , and are all coprime to each other.

Non Primitive Triplets : When both and are odd or even, then , , and will be even, and the triple will not be primitive; however, dividing , , and by will yield a primitive triple when and are coprime and both odd.

Putting above values in the Pythagorean Theorem we will get,

Proof

We know that,

As , and are all coprime to each other and their fraction, will be rational. So if we consider,

Then,

Solving for and ,

Hence is the result be obtained above,

Examples

There are infinite Pythagorean triplets as there are infinite odd numbers.

There are 16 primitive Pythagorean triples with c ≤ 100:

Code

Naive Approach

/**
 * A naive pythagorean triplet generator
 *
 * @param limit
 *            : maximum value of c
 *
 * @return a list of all the pythagorean triplets
 */
public static ArrayList<int[]> naivePythagoreanTripletsGenerator(int limit) {
  ArrayList<int[]> triplets = new ArrayList<>();

  for (int a = 3; a <= limit; a++) {
    for (int b = a + 1; b <= limit; b++) {
      for (int c = b + 1; c <= limit; c++) {
        if (a * a + b * b == c * c) {
          triplets.add(new int[] { a, b, c });
        }
      }
    }
  }
  return triplets;
}

Euclid’s Formula for Primitive Triplets

/**
 * A function to calculate the gcd of 2 given numbers
 *
 * @param a
 *            first number
 * @param b
 *            second number
 * @return gcd of two numbers
 */
public static int gcd(int a, int b) {
  return a == 0 ? b : gcd(b % a, a);
}

/**
 * A function for Euclid's Formula for pythagorean triplet generator
 *
 * @param limit
 *            : maximum value of c
 *
 * @return a list of all the pythagorean triplets
 */
public static ArrayList<int[]> pythagoreanTripletsGenerator(int limit) {
  ArrayList<int[]> triplets = new ArrayList<>();

  for (int m = 1; m < Math.sqrt(limit); m++) {
    for (int n = 1 + (m % 2); n < m; n += 2) {
      int a = m * m - n * n;
      int b = 2 * m * n;
      int c = m * m + n * n;
      if (c > limit) {
        break;
      }
      if (gcd(gcd(a, b), c) == 1) {
        triplets.add(new int[] { a, b, c });

      }
    }
  }

  return triplets;
}

References and Further Readings

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...