, ( ). , , , , Python.
-, , ( , , N itertools.count(1)). , :
from itertools import count
def RZ():
x=count(1)
y=RZ()
while True:
yield next(x)
yield next(y)
:
>>> gen = RZ()
>>> for i in range(1, 21):
print i, next(gen)
1 1
2 1
3 2
4 1
5 3
6 2
7 4
8 1
9 5
10 3
11 6
12 2
13 7
14 4
15 8
16 1
17 9
18 5
19 10
20 3
, , . , python ( 2.7):
def getDepth(gen):
depth = 0
while gen:
depth += 1
gen = gen.gi_frame.f_locals.get("y")
return depth
(, , ):
>>> for i in range(1, 21):
print i, getDepth(gen), next(gen)
1 1 1
2 2 1
3 3 2
4 3 1
5 4 3
6 4 2
7 4 4
8 4 1
9 5 5
10 5 3
11 5 6
12 5 2
13 5 7
14 5 4
15 5 8
16 5 1
17 6 9
18 6 5
19 6 10
20 6 3
. , , N- , ceil(log(N, 2)) + 1.
Python ( ) 100 . , 2 ^ 99 + 1 (= 633,825,300,114,114,774,748,351,602,689). , .