As the wiki now explains, (1, 1) -> (9, 3) -> (3, 1) -> (na, nb) -> (a, b), originally found by me, is the "mathematical" approach you seek (it works for any a, b)
I would like to note that you can solve "The Problem" without code, by getting 21 pink, 63 green and then dividing by 3, which is much simpler.