Skip to content

Commit ba2cd3b

Browse files
committed
Merge pull request #150 from rivese/generator
Generator assignment
2 parents 4cc225f + 0ba7eac commit ba2cd3b

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
3+
4+
import itertools
5+
6+
7+
def intsum():
8+
u"""Generate a sequence that adds the next integer."""
9+
10+
a = 0
11+
yield a
12+
13+
for i in itertools.count(1, 1):
14+
a = a + i
15+
yield a
16+
17+
18+
def doubler():
19+
u"""Generate a sequence that doubles the previous value."""
20+
21+
a = 1
22+
yield a
23+
24+
while True:
25+
a = 2 * a
26+
yield a
27+
28+
29+
def fib():
30+
u"""Generate the fibonacci series."""
31+
32+
a, b = 0, 1
33+
34+
while True:
35+
a, b = b, a + b
36+
yield a
37+
38+
39+
def prime():
40+
u"""Simply generate the prime numbers."""
41+
42+
for i in itertools.count(2, 1):
43+
for a in range(2, i):
44+
if i%a == 0:
45+
break
46+
else:
47+
yield i
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+

0 commit comments

Comments
 (0)