pythagorean triples

Pythagorean_Triples

Finding the pythagorean triples.

A pythagorean triple is a set of three positive integers A, B and C such that the equation C2 = A2 + B2 always holds true.

Properties of pythagorean triple

  • If A, B and C form a pythagorean triple, then A < B < C holds true.
  • It always has two odd numbers and one even number or all even numbers.
  • If the smallest number in the pythagorean triple is even, say A, then the other 2 odd numbers would be (A/2)2-1 and (A/2)2+1.
    Example 1: (8, ((8/2)2-1), ((8/2)2+1)) i.e (8, 15, 17)
    Example 2: (12, ((12/2)2-1), ((12/2)2+1)) i.e (12, 35, 36)
  • If the smallest numberin the pythagorean triple is odd, say A, then the other 2 numbers would be (A2)/2 and (A2)/2+1.
    Example 1: (9, (92)/2, (92)/2+1) i.e (9, 40, 41)
    Example 2: (3, (32)/2, (32)/2+1) i.e (3, 4, 5)







Pythagorean triples implementation

# Finding pythagorean triple where the biggest number is <= upto
def Get_Pythagorean_Triples(upto):

    # If (a, b, c) form a pythagorean triple, then (a < b < c) must hold.
    # Thus 'a' has to be less than upto. 'b' should be greater than 'a' and 'c' should be 
    # greater than 'b'.
    for a in range(1, upto):
        a_square = a*a
        b = a + 1; # b has to be greater than a
        c = b + 1; # c has to be greater than b
        while c <= upto:

            c_square = a_square + b*b 

            # Increment c till c_square = a_square + b_square hold true for a given value of a and b.
            while c*c < c_square:
                c += 1

            # Triple found
            if c*c == c_square and c <= upto:
                print(str(a)+" "+str(b)+" "+str(c))
                    
            # Increment b only after all values of c are tried for c_square = a_square + b_square for a 
            # given value of a
            b += 1

upto = 100 
Get_Pythagorean_Triples(upto)

Output

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
28 96 100
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
60 80 100
65 72 97
#include<iostream>
using namespace std;

// Finding pythagorean triple where the biggest number is <= upto
void Get_Pythagorean_Triples(int upto) {

     // If (a, b, c) form a pythagorean triple, then (a < b < c) must hold.
     // Thus a has to be less than upto. b should be greater than a and c should be 
     // greater than b.
     int a, b, c;
     for(a = 1; a < upto; ++a) {
         int a_square = a*a;
         b = a + 1; // b has to be greater than a
         c = b + 1; // c has to be greater than b
         while(c <= upto) {
             int c_square = a_square + b*b;
             // Increment c till c_square = a_square + b_square hold true for a given value of a and b.
             while(c*c < c_square) {
                 ++c;
             }   
             // Triple found
             if(c*c == c_square && c <= upto) {
                 cout << a << " " << b << " " << c << endl;
             }
             // Increment b only after all values of c are tried for c_square = a_square + b_square for a 
             // given value of a
             ++b;
         }   
     }   
}

int main(){

    int upto = 100;
    Get_Pythagorean_Triples(upto);
    return 0;
} 

Output

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
28 96 100
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
60 80 100
65 72 97



Copyright (c) 2019-2021, Algotree.org.
All rights reserved.