#!/usr/bin/python # -*- coding: utf-8 -*- """Add angles expressed as ratios. It’s common to express angles in terms of their tangents; as, a 6:1 angle, or a 1:10 slope, or a 3:4:5 triangle. After a Twitter discussion with @Blouchtika about Wildberger’s rational trigonometry and a recently discovered Babylonian tablet of Pythagorean triples, I realized that there’s a simple way to add angles expressed in this form, and it produces a new Pythagorean triple, given any two Pythagorean triples! Also, given two rational slopes, the resulting angle sum is also rational, expressed as a slope. This code computes that angle sum exactly as a slope, using strictly integer math. """ import sys def main(argv): _, a, b, c, d = argv a, b, c, d = map(int, [a,b,c,d]) print(angle_sum((a,b), (c,d))) def angle_sum(x, y): (a, b), (c, d) = x, y return reduced(c*a - b*d, c*b + a*d) def reduced(x, y): d = gcd(x, y) return x//d, y//d gcd = lambda x, y: x if y==0 else gcd(y, x % y) if __name__ == '__main__': main(sys.argv)