1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 | """Iterative version of Fibonacci."""
|
4 |
|
5 | i = 0
|
6 | n = 10
|
7 | a = 0
|
8 | b = 1
|
9 |
|
10 | while 1: # Slightly easier to compile than 'while True:'
|
11 |
|
12 | # Artificial change to test 'continue'
|
13 | if i == 0:
|
14 | i = i + 1
|
15 | continue
|
16 |
|
17 | print(b)
|
18 |
|
19 | # NOTE: This would generate BUILD_TUPLE and UNPACK_SEQUENCE bytecodes.
|
20 | #a, b = b, a+b
|
21 |
|
22 | tmp = a
|
23 | a = b
|
24 | b = tmp + b
|
25 |
|
26 | i = i + 1 # Don't use augmented assignment
|
27 | if i == n:
|
28 | break
|
29 |
|
30 | print('Done fib_iterative.py') # To make sure we implemented 'break' properly
|