fibonacci.py


Below is the syntax highlighted version of fibonacci.py from §1.3 Conditionals and Loops.


#-----------------------------------------------------------------------
# fibonacci.py
#-----------------------------------------------------------------------

import stdio
import sys

# Accept integer n as a command-line argument. Write to standard
# output the first n Fibonacci numbers.

n = int(sys.argv[1])

f = 0
g = 1

for i in range(0, n):
    f = f + g
    g = f - g
    stdio.writeln(f)
    
#-----------------------------------------------------------------------

# python fibonacci.py 16
# 1
# 1
# 2
# 3
# 5
# 8
# 13
# 21
# 34
# 55
# 89
# 144
# 233
# 377
# 610
# 987


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Fri Oct 20 20:45:16 EDT 2017.