Project Euler #91: Right triangles with integer coordinates

  • + 0 comments
    Python
    from math import gcd
    
    N = int(input())
    
    result = 3 * N ** 2
    for x in range(1, N+1):
        for y in range(1, x+1):
            den = gcd(x, y)
            d_x, d_y = x // den, y // den
            
            found = 0
            n_x, n_y = x - d_y, y + d_x
            while n_x >= 0 and n_y <= N:
                found += 1
                n_x, n_y = n_x - d_y, n_y + d_x
                
            n_x, n_y = x + d_y, y - d_x
            while n_y >= 0 and n_x <= N:
                found += 1
                n_x, n_y = n_x + d_y, n_y - d_x
                
            result += found * 2 if x != y else found
            
    print(result)