improved solutions 1 and 3, using xrange instead of range