project euler problem 75 https projecteuler netproblem75 it turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way but there are many more examples 12 cm 3 4 5 24 cm 6 8 10 30 cm 5 12 13 36 cm 9 12 15 40 cm 8 15 17 48 cm 12 16 20 in contrast some lengths of wire like 20 cm cannot be bent to form an integer sided right angle triangle and other lengths allow more than one solution to be found for example using 120 cm it is possible to form exactly three different integer sided right angle triangles 120 cm 30 40 50 20 48 52 24 45 51 given that l is the length of the wire for how many values of l 1 500 000 can exactly one integer sided right angle triangle be formed solution we generate all pythagorean triples using euclid s formula and keep track of the frequencies of the perimeters reference https en wikipedia orgwikipythagoreantriplegeneratingatriple return the number of values of l limit such that a wire of length l can be formmed into an integer sided right angle triangle in exactly one way solution50 6 solution1000 112 solution50000 5502 return the number of values of l limit such that a wire of length l can be formmed into an integer sided right angle triangle in exactly one way solution 50 6 solution 1000 112 solution 50000 5502
from collections import defaultdict
from math import gcd


def solution(limit: int = 1500000) -> int:
    
    frequencies: defaultdict = defaultdict(int)
    euclid_m = 2
    while 2 * euclid_m * (euclid_m + 1) <= limit:
        for euclid_n in range((euclid_m % 2) + 1, euclid_m, 2):
            if gcd(euclid_m, euclid_n) > 1:
                continue
            primitive_perimeter = 2 * euclid_m * (euclid_m + euclid_n)
            for perimeter in range(primitive_perimeter, limit + 1, primitive_perimeter):
                frequencies[perimeter] += 1
        euclid_m += 1
    return sum(1 for frequency in frequencies.values() if frequency == 1)


if __name__ == "__main__":
    print(f"{solution() = }")