########################################################################## # Copyright (C) 2006 Jaap Spies, jaapspies@gmail.com # Copyright (C) 2006 William Stein, wstein@gmail.com # # Distributed under the terms of the GNU General Public License (GPL): # # http://www.gnu.org/licenses/ ########################################################################## def fib(): """ generates an "infinity" of Fibonacci numbers, starting with 0 """ x, y = 0, 1 yield x while 1: x, y = y, x+y yield x f = fib() a = [f.next() for i in range(1002)] def A000045(n): r""" returns Fibonacci number with index $n \le 1001$, offset 0,4 S. Plouffe, Project Gutenberg, The First 1001 Fibonacci Numbers http://ibiblio.org/pub/docs/books/gutenberg/etext01/fbncc10.txt We have one more. Our first Fibonacci number is 0. INPUT: n -- non negative integer OUTPUT: integer -- function value EXAMPLES: sage: A000045(4) 3 sage: A000045(10) 55 sage: A000045(51) 20365011074 sage: A000045(-1) AUTHOR: - Jaap Spies (2007-01-05) """ if n >= 0 and n <= 1001: return a[n] def A000045_list(N): r""" returns a list of the first $N$ Fibonacci numbers INPUT: N -- non negative integer OUTPUT: list -- Fibonacci numbers EXAMPLES: sage: A000045_list(9) [0, 1, 1, 2, 3, 5, 8, 13, 21] sage: A000045_list(0) [] sage: A000045_list(1) [0] AUTHOR: - Jaap Spies (2007-01-05) """ return a[:N]