# Pythagorean Triples

A Pythagorean triplet is a set of three numbers a, b, and c where `a^2 + b^2 = c^2`. In this Kata, you will be tasked with finding the Pythagorean triplets whose product is equal to `n`, the given argument to the function `pythagorean_triplet`.

## Your task

In this Kata, you will be tasked with finding the Pythagorean triplets whose product is equal to `n`, the given argument to the function, where `0 < n < 10000000`


## Examples

One such triple is `3, 4, 5`. For this challenge, you would be given the value `60` as the argument to your function, and then it would return the Pythagorean triplet in an array `[3, 4, 5]` which is returned in increasing order. `3^2 + 4^2 = 5^2` since `9 + 16 = 25` and then their product (`3 * 4 * 5`) is `60`.


More examples:

| **argument** | **returns** |
| ---------|---------|
| 60       | [3, 4, 5] |
| 780      | [5, 12, 13] |
| 2040     | [8, 15, 17] |

Difficulty: introductory

Starter code:

def pythagorean_triplet(n):
	

Solutions:
def pythagorean_triplet(n):
    for a in range(3, n):
        for b in range(a+1, n):
            c = (a*a + b*b)**0.5
            if a*b*c > n:
                break
            if c == int(c) and a*b*c == n:
                return [a, b, c]

def getTriplets(n):
    divs = {y for x in range(1,int(n**.5)+1) for y in (x, n//x) if not n%x}
    for a in divs:
        for b in divs:
            if n/a/b in divs:
                yield sorted((a, b, n/(a*b)))

def pythagorean_triplet(n):
    return next( [a,b,c] for a,b,c in getTriplets(n) if a**2 + b**2 == c**2 )

triples = [(3, 4, 5), (5, 12, 13), (8, 15, 17), (7, 24, 25), (20, 21, 29), (12, 35, 37),
           (9, 40, 41), (28, 45, 53), (11, 60, 61), (16, 63, 65), (33, 56, 65), (48, 55, 73),
           (13, 84, 85), (36, 77, 85), (39, 80, 89), (65, 72, 97), (20, 99, 101), (60, 91, 109),
           (15, 112, 113), (44, 117, 125), (88, 105, 137), (17, 144, 145), (24, 143, 145),
           (51, 140, 149), (85, 132, 157), (119, 120, 169), (52, 165, 173), (19, 180, 181),
           (57, 176, 185), (104, 153, 185), (95, 168, 193), (28, 195, 197), (84, 187, 205),
           (133, 156, 205), (21, 220, 221), (140, 171, 221), (60, 221, 229), (105, 208, 233),
           (120, 209, 241), (32, 255, 257), (23, 264, 265), (96, 247, 265), (69, 260, 269),
           (115, 252, 277), (160, 231, 281), (161, 240, 289), (68, 285, 293)]

def pythagorean_triplet(n):
    for a, b, c in triples:
        a0, b0, c0 = a, b, c
        
        while a * b * c < n:
            a += a0
            b += b0
            c += c0
        
        if a * b * c > n:
            continue
        else:
            return [a, b, c]

from itertools import combinations
from math import hypot

limit = 10000000
candidates = ((a,b,hypot(a,b)) for a,b in combinations(range(1, 1000), 2))
D = {a*b*int(c):[a,b,int(c)] for a,b,c in candidates if c.is_integer() and a*b*c < limit}
pythagorean_triplet = D.__getitem__

def triplet(n):
    for i in range(int(n**0.333), 1, -1):
        if n % i:
            continue
        c = n // i
        for j in range(int(c**0.5), i, -1):
            if c % j or j == c // j:
                continue
            yield (i, j, c // j)

def pythagorean_triplet(n):
    for a, b, c in triplet(n):
        if a**2 + b**2 == c**2:
            return [a, b, c]

def pythagorean_triplet(n):
    for k in range(1, int(n ** (1 / 3))):
        if n % k ** 3: continue
        for p in range(1, int((n // k ** 3) ** .2) + 1):
            for q in range(1, p):
                a, b, c = p*p - q*q, 2*p*q, p*p + q*q
                if k**3 * a * b * c == n : return sorted([k*a, k*b, k*c])

def pythagorean_triplet(n):
    for z in range(1, int(n**0.5) + 1): # z is hypotenuse
        y = n / z # y is a * b
        if y.is_integer():
            x = (z**2 + 2*y) ** 0.5 # (a + b)
            if x.is_integer():
                for a in range(1, int(x)):
                    b = x - a
                    if a*b == y:
                        return sorted([a,b,z])

def factors(n, limit):
  return [i for i in range(2, limit+1) if n % i == 0]

def pythagorean_triplet(n):
  for x in factors(n, int(n**(1/3))):
    nn = n // x
    for y in factors(nn, int(nn**(1/2))):
      z = nn // y
      if x*x + y*y == z*z :
        return [x, y, z]

pythagorean_triplet=lambda n: [[d*round((n/k)**(1/3)) for d in v] for k,v in {60: [3, 4, 5], 780: [5, 12, 13], 2040: [8, 15, 17], 4200: [7, 24, 25], 14760: [9, 40, 41], 15540: [12, 35, 37], 40260: [11, 60, 61], 65520: [16, 63, 65], 66780: [28, 45, 53], 92820: [13, 84, 85], 12180: [20, 21, 29], 120120: [33, 56, 65], 192720: [48, 55, 73], 235620: [36, 77, 85], 277680: [39, 80, 89], 328860: [60, 63, 87],  453960: [65, 72, 97]}.items() if round((n // k)**(1/3))**3 == n / k][0]

def pythagorean_triplet(n):
    from math import sqrt
    def divisors(n):
        divs = []
        for i in range(2, int(sqrt(n))+1):
            if n % i == 0:
                divs.append(i)
        divs.sort()
        return divs
    divs = divisors(n)
    for a in divs:
        for b in divs:
            c = n // (a * b)
            if a * a + b * b == c * c:
                return [a, b, c]