# Pythagorean_triads (c) 2026 Baltasar MIT License <jbgarcia@uvigo.es>


from random import randint as rnd
import random


def crea_random_triads(l, max):
    """Creates a list of random triads.
        :param l: the list in which to add the triads.
        :param max: the number of triads, and each number upper limit.
        :return: a list with max triads added.
    """
    for _ in range(max):
        l.append(tuple([rnd(1, max), rnd(1, max), rnd(1, max)]))
    ...
...


def crea_random_triad(max):
    """Creates a single random triad.
        :param max: the upper limit for each number.
        :return: a tuple.
    """
    return tuple([rnd(1, max), rnd(1, max), rnd(1, max)])
...


def crea_semi_random_triads(l, max):
    """Creates a list of random triads, using Euclides' algorithm.
        :param l: the list in which to add the triads.
        :param max: the number of triads, and each number upper limit.
        :return: a list with max triads added.
    """
    for _ in range(max): 
        a = -1
        b = 0
    
        while a < b:
            a = rnd(1, max)
            b = rnd(1, max)
        ...
    
        x = (a ** 2) - (b ** 2)
        y = 2 * a * b
        z = (a ** 2) + (b ** 2)
        l.append(tuple([x, y, z]))
    ...
...


def filter_real_triads(l):
    """Filters which triads are pythagorean.
        :return: a list with the pythagorean triads in l.
    """
    return list(filter(lambda t: ((t[0] ** 2) + (t[1] ** 2) == (t[2] ** 2)), l))
...


if __name__ == "__main__":
    max = 100
    random.seed()
    
    print("\n# Random triads:")
    l1 = [(3, 4, 5)]
    pl1 = list(l1)
    repetitions = 0
    while repetitions < max**2:
        t = crea_random_triad(max)
        l1.append(t)
        if len(filter_real_triads([t])) > 0:
            pl1.append(t)
        ...
        repetitions += 1
    ...
    print(l1[:20], "...")
    print("\n# Pythagorean triads in random list")
    print(f"{pl1}\nPercentage: {(len(pl1)/len(l1)) * 100:05.2f}%")
    print(f"Repetitions: {repetitions}")
    
    print("\n# Semi-random triads:")
    l2 = [(3, 4, 5)]
    crea_semi_random_triads(l2, max)
    print(l2)
    
    print("\n# Pythagorean triads in semi-random list")
    pl2 = filter_real_triads(l2)
    print(f"{pl2}\nPercentage: {(len(pl2)/len(l2)) * 100:05.2f}%")
...
