> for k in range(max): This line creates a list of max numbers. Each number takes up several bytes of RAM(4+?). So 20 million numbers is over 80MB RAM being used. You probably should investigate generators for this kind of thing, or at least use xrange() instread of range() Alan G.