© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_16

16. Pythagorean Triples

Ron Dai1 
(1)
Seattle, WA, USA
 

The Pythagorean Theorem is well known among elementary to middle school students, given its elegantly looking equation that applies to all right triangles.

Math: Pythagorean Triples

Inside a right triangle, a and b are the two legs, and c is the hypotenuse.
  • a2 + b2 = c2

When a, b, and c are positive integers satisfying the Pythagorean Theorem, (a, b, c) are called “Pythagorean Triples.”. Obviously (3, 4, 5) is the first Pythagorean triple, followed by (5, 12, 13), (6, 8, 10), and so forth. The number of Pythagorean triples is infinite.

Example

So, how do we find all the Pythagorean triples below 100?

Answer

We can multiply (3, 4, 5) with any integer number to get (6, 8, 10), (9, 12, 15), …, (57, 76, 95). We can use (5, 12, 13) as another base triple to get (10, 24, 26), …, (35, 84, 91). And so on.

But this approach will require us to first find out all the base Pythagorean triples. Thus, we would essentially have to check every positive integer below 100 for a, and then figure out b and c, assuming a < b < c. By the way, a = b will not be possible. However, with a programming approach, it is no longer a challenging math problem.

This is the method to find out all the possible triples (a, b, c) satisfying the Pythagorean Theorem.
       private static int allPythagoreanNumbers(int upperBound) {
              int count = 0;
              for(int a = 1; a < upperBound; a++) {
                     for(int b = a; b < upperBound; b++) {
                            for(int c = b; c < upperBound; c++) {
                                   if (a * a + b * b == c * c) {
                                          System.out.println("("+a+", "+b+", "+c+")");
                                          count++;
                                   }
                            }
                     }
              }
              return count;
       }
       public static void main(String[] args) {
              System.out.println("Total count: " + allPythagoreanNumbers(100));
       }
It will output something like what follows:
  • (3, 4, 5)

  • (5, 12, 13)

  • (6, 8, 10)

  • (7, 24, 25)

  • (8, 15, 17)

  • (9, 12, 15)

  • (9, 40, 41)

  • (10, 24, 26)

  • (11, 60, 61)

  • (12, 16, 20)

  • (12, 35, 37)

  • (13, 84, 85)

  • (14, 48, 50)

  • (15, 20, 25)

  • (15, 36, 39)

  • (16, 30, 34)

  • (16, 63, 65)

  • (18, 24, 30)

  • (18, 80, 82)

  • (20, 21, 29)

  • (20, 48, 52)

  • (21, 28, 35)

  • (21, 72, 75)

  • (24, 32, 40)

  • (24, 45, 51)

  • (24, 70, 74)

  • (25, 60, 65)

  • (27, 36, 45)

  • (28, 45, 53)

  • (30, 40, 50)

  • (30, 72, 78)

  • (32, 60, 68)

  • (33, 44, 55)

  • (33, 56, 65)

  • (35, 84, 91)

  • (36, 48, 60)

  • (36, 77, 85)

  • (39, 52, 65)

  • (39, 80, 89)

  • (40, 42, 58)

  • (40, 75, 85)

  • (42, 56, 70)

  • (45, 60, 75)

  • (48, 55, 73)

  • (48, 64, 80)

  • (51, 68, 85)

  • (54, 72, 90)

  • (57, 76, 95)

  • (60, 63, 87)

  • (65, 72, 97)

  • Total count: 50

This is just one of many demonstrations of how we can use programs to solve problems.

Problems

  1. 1.

    In the example, we used three for-loops to iterate a, b, c from 1 through 99. How do you improve it by reducing to two for-loops?

     
  2. 2.

    Using the idea from the example, how do we find out all the Pythagorean primes smaller than 100? Pythagorean primes are explained below.

     

Math: Pythagorean Primes

Pythagorean primes are the sum of two squares. And, it needs to be in form of 4n + 1, where n is a positive integer. Examples of Pythagorean primes are 5, 13, 17, 29, 37 and 41.

Hine

Take advantage of the example code and see how to make small changes to find a solution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.184.90