Author steve21
Recipients LambertDW, rhettinger, steve21
Date 2009-01-27.01:42:17
SpamBayes Score 0.00304533
Marked as misclassified No
Message-id <1233020539.44.0.269039576151.issue5032@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a couple of functions I use with count and step:

def cf_e():
    '''return: (iterator) the infinite continued fraction for e
    e=[2; 1, 2, 1, 1, 4, 1, 1, 6, 1 , ... , 1, 2k, 1, ...]
    '''
    yield 2
    for k in itertools.count(2, 2):
        yield 1
        yield k
        yield 1


def prime_factors(n):
    '''n: (int > 1)
    return: (list) the sorted list of tuples (p,e) of prime factors of n
            p is a prime factor, e is the number of times the factor is used
    '''
    ret = []
    if n <= 1:
        return ret

    # factors: use known (small) primes, then possible primes (odd numbers)
    for factor in itertools.chain([2,3,5,7,11], itertools.count(13, 2)):
        if factor*factor > n:
            if n != 1:
                ret += [(n, 1)]
            break
        for e in itertools.count(0):
            div, mod = divmod(n, factor)
            if mod != 0:
                break
            n = div
        if e >= 1:
            ret += [(factor, e)]
    return ret
History
Date User Action Args
2009-01-27 01:42:19steve21setrecipients: + steve21, rhettinger, LambertDW
2009-01-27 01:42:19steve21setmessageid: <1233020539.44.0.269039576151.issue5032@psf.upfronthosting.co.za>
2009-01-27 01:42:17steve21linkissue5032 messages
2009-01-27 01:42:17steve21create