-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
78 lines (61 loc) · 1.51 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from itertools import islice, takewhile, count
from math import sqrt
__author__ = 'rafa'
# https://docs.python.org/2/library/itertools.html
def nth(iterable, n, default=None):
"""Returns the nth item or a default value"""
return next(islice(iterable, n, None), default)
def iter_primes():
"""yields prime numbers"""
primes = [2]
yield 2
n = 3
while True:
test_primes = takewhile(lambda x: x <= sqrt(n), primes)
if all(n % p != 0 for p in test_primes):
yield n
primes.append(n)
n += 2
def list_primes(limit):
"""
eratostenes sieve
More efficient than iter_primes when whe know the limit.
"""
sieve = [False]*2 + [True] * (limit-2)
n = 2
while n <= sqrt(limit):
if sieve[n]:
yield n
for m in xrange(n**2, limit, n): # multiples
sieve[m] = False # mark multiples as non prime
n += 1
while n < limit:
if sieve[n]:
yield n
n += 1
def iter_factors(n):
x = n
primes = iter_primes()
p = next(primes)
while x != 1:
while x % p == 0: # p divides x
yield p
x /= p
p = next(primes)
def iter_fibonacci():
a, b = 1, 2
yield a
yield b
while True:
a, b = b, a+b
yield b
def iter_triangular_numbers():
n = 0
for i in count(1):
n += i
yield n
def iter_digits(n, base=10):
q = n
while q != 0:
q, r = divmod(q, base)
yield r