Use xrange in the generator function

xrange is in turn a generator, so we can further reduce memory use.