[issue5032] itertools.count step
steve21
report at bugs.python.org
Tue Jan 27 02:42:19 CET 2009
steve21 <steve872929-bv at yahoo.com.au> added the comment:
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
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5032>
_______________________________________
More information about the Python-bugs-list
mailing list