prob 9 take 1
faster prob 9