omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / pythagorean-triples / oracle.py
| 1 | # Independent reference for "Pythagorean triples" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | import math |
| 4 | # Berggren's tree of primitive triples: every primitive triple appears once, children have larger perimeters |
| 5 | M = [((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))] |
| 6 | TOP = 10 ** 7 |
| 7 | per = [] |
| 8 | stack = [(3, 4, 5)] |
| 9 | while stack: |
| 10 | t = stack.pop() |
| 11 | p = sum(t) |
| 12 | if p > TOP: |
| 13 | continue |
| 14 | per.append(p) |
| 15 | for m in M: |
| 16 | stack.append(tuple(sum(r[i] * t[i] for i in range(3)) for r in m)) |
| 17 | for e in range(2, 8): |
| 18 | lim = 10 ** e |
| 19 | print(f"perimeter <= {lim}: {sum(lim // p for p in per if p <= lim)} triples, {sum(1 for p in per if p <= lim)} primitive") |