6 of 8
added 17 characters in body

Buffering output on

As per the help, here's what I'm looking for from this post: best practice and practical advice about (what I think is describable as) buffering the output of a function.

So I'm working on the third Euler problem and I've written a simple little function that returns the list of prime factors of a given number.

The number for which I have to get all the factors is over 6 billion, so building this huge freakin list of numbers in memory struck me as wonky.

I decided I'd build a factor function that generates output by the chunk and spits it out so that I can write it to a text file chunk by chunk. A buffered_factor function:

Ok, I've decided that doing the 'buffered' thing isn't needful. The computation is just going to take a while, and making my factor function a generator will keep my program light in memory.

Here's the prime sieve:

def is_prime(n):
    if n == 1:
        return False
    if n == 2:
        return True
    
    for i in range(2, int(math.ciel(math.sqrt(n))):
        if n % i:
            return False
        return True

And the factoratorizer:

def factor(num, chunk_size=1000):
    """Get factors of given number.

    :param num: get us them there factors o this number here
    :type num: integer
    :param chunk_size: number of operations to work through before crapping out a list
    :rtype: list of factors
    :raises: ValueError on num < 1
    """
    if num < 1:
        raise ValueError('Given %s. Numbers >= 1, please' % num)

    for i in (n + 1 for n in xrange(num)):
        if num % i == 0:
            if is_prime(i):
                yield i

I'm trying to use it thusly:

import os
from factor import buffered_factor
TARGET = 600851475143
OUTFILE = 'factors.txt'

if os.path.isfile(OUTFILE):
    os.remove(OUTFILE)

with open(OUTFILE, 'a') as f:
   for output in buffered_factor(TARGET):
       f.write(str(output))
       f.flush()

Nothing's being written to file, so obviously I'm doing something wrong.