Solved #3 in ruby, but it's way too slow.
Tweaked ruby #3, still way too slow.