gcd/lcd approach for prob 5, cleanup