Talk:Stern-Brocot sequence: Difference between revisions

Content added Content deleted
(→‎deque over list?: remove unnecessary call to sum in deque example.)
Line 37: Line 37:
...: sb = deque([1, 1])
...: sb = deque([1, 1])
...: while True:
...: while True:
...: sb += [sum(sb[0] + sb[1]), sb[1]]
...: sb += [sb[0] + sb[1], sb[1]]
...: yield sb.popleft()
...: yield sb.popleft()
...:
...:
Line 56: Line 56:


%time [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot2())) for occur in (list(range(1, 11)) + [2500])]
%time [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot2())) for occur in (list(range(1, 11)) + [2500])]
Wall time: 2.46 s
Wall time: 215 ms
Out[33]: [1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 152931]
Out[37]: [1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 152931]


%time [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot2())) for occur in (list(range(1, 11)) + [2500])]
%time [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot2())) for occur in (list(range(1, 11)) + [2500])]
Wall time: 2.49 s
Wall time: 225 ms
Out[34]: [1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 152931]
Out[38]: [1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 152931]


%time [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot2())) for occur in (list(range(1, 11)) + [2500])]
%time [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot2())) for occur in (list(range(1, 11)) + [2500])]
Wall time: 2.48 s
Wall time: 223 ms
Out[35]: [1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 152931]</lang>
Out[39]: [1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 152931]</lang>


The deque is faster, (and the margin increases for later members of the series).
The deque is faster, (and the margin increases for later members of the series).