Home > Blockchain >  Repeating digits in the Pythagorean triad
Repeating digits in the Pythagorean triad

Time:12-18

I have written a fun that counts pitagorian triad:

def triad(N):
    for a in range(1,N):
        for b in range(1,N):
            for c in range(1,N):
                if a*a b*b==c*c and a!=b and b!=a:
                    print(a,b,c)
                    print('-'*10)
triad(20)

but the output is:

3 4 5
----------
4 3 5
----------
5 12 13
----------
6 8 10
----------
8 6 10
----------
8 15 17
----------
9 12 15
----------
12 5 13
----------
12 9 15
----------
15 8 17
----------

and I don't want:

3 4 5
----------
4 3 5
--------- 

and

6 8 10
----------
8 6 10
----------

How to get rid of repeating digits?

I tried a!=b and b!=a

CodePudding user response:

One approach is to start b as a 1 and c as a 2:

def triad(N):
    for a in range(1, N):
        for b in range(a   1, N):
            for c in range(a   2, N):
                if a * a   b * b == c * c:
                    print(a, b, c)
                    print('-' * 10)

triad(20)

Output

3 4 5
----------
5 12 13
----------
6 8 10
----------
8 15 17
----------
9 12 15
----------

CodePudding user response:

Another solution without the 3rd loop and much efficient

import math

def triad(N):
    for a in range(1, N):
        for b in range(a   1, N):
            sq = a * a   b * b
            c = int(math.sqrt(sq))
            if sq == c * c and c > b:
                print(a, b, c)
                print('-' * 10)

triad(20)

CodePudding user response:

You could use the "tree of primitive Pythagorian triples":

def pythagorianTriples(n):
    def mul(m, a):  # Multiplication of matrix with vector
        return [sum(x * y for x, y in zip(row, a)) for row in m]
                
    matrices = (
        (
            (1, -2, 2),
            (2, -1, 2),
            (2, -2, 3)
        ), (
            (1, 2, 2),
            (2, 1, 2),
            (2, 2, 3)
        ), (
            (-1, 2, 2),
            (-2, 1, 2),
            (-2, 2, 3)
        )
    )
    q = [[3, 4, 5]]
    while q:
        a = q.pop()
        b = sorted(a)
        if b[-1] <= n:
            # yield this triple and multiples of it
            while b[-1] <= n:
                yield b
                for i in range(3):
                    b[i]  = a[i]
            # add 3 more triples to the list
            q.extend(mul(m, a) for m in matrices)

for res in pythagorianTriples(20):
    print(res)

This outputs:

[3, 4, 5]
[6, 8, 10]
[9, 12, 15]
[12, 16, 20]
[8, 15, 17]
[5, 12, 13]

CodePudding user response:

Another approach would be:

def triad(N):
    for a in range(1,N):
        for b in range(1,N):
            for c in range(1,N):
                if (a*a   b*b) == c*c and a < b:
                    print(a,b,c)
                    print(10*"-")

triad(20)

Output:

3 4 5
----------
5 12 13
----------
6 8 10
----------
8 15 17
----------
9 12 15
----------

CodePudding user response:

Another approach using itertools

One thing about combinations is it returns the items in the same order as the iterable so it's safe to assume that the 3rd item is the only possible hypotenuse.

from itertools import combinations
from math import sqrt

def triad(N):
    return [c for c in combinations(range(1, N), 3) 
            if sqrt(c[0]**2   c[1]**2) == c[2]]
        
print(triad(20))

Output:

[(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15)]

CodePudding user response:

To avoid repeating digits in the Pythagorean triad, you can add a condition to your code to check that the values of a, b, and c are all distinct. One way to do this is to create a set of the values and check that the length of the set is equal to 3:

def triad(N):
    for a in range(1,N):
        for b in range(1,N):
            for c in range(1,N):
                if a*a b*b==c*c and len(set((a, b, c))) == 3:
                    print(a,b,c)
                    print('-'*10)

triad(20)

This will only print Pythagorean triads where the values of a, b, and c are all distinct.

  • Related