2 Bucket Problem Solver

Problem Statement : Suppose you have a "p" liter bucket and a "q" liter bucket. There is no other conatainer and neither do the buckets have measurement lines on them. How could you measure exactly "r" liters using only those buckets and you have as much extra water as you need ?


Rule 1 : p > q
Rule 2 : p > r
Rule 3 : r mod gcd(p, q) = 0

Illustrations are not indicative of actual size of conatiners but the rather represent the percentage difference