Generators, Recursion, and Fractals
Generators, Recursion, and Fractals
1 Generators computing a list of Fibonacci numbers defining a generator with yield putting yield in the function fib
2 Recursive Functions computing factorials recursively computing factorials iteratively
3 Recursive Images some examples recursive definition of the Cantor set recursive drawing algorithm
MCS 260 Lecture 41 Introduction to Computer Science
Jan Verschelde, 22 April 2016
Intro to Computer Science (MCS 260)
generators and recursion
L-41 22 April 2016 1 / 36
Generators, Recursion, and Fractals
1 Generators computing a list of Fibonacci numbers defining a generator with yield putting yield in the function fib
2 Recursive Functions computing factorials recursively computing factorials iteratively
3 Recursive Images some examples recursive definition of the Cantor set recursive drawing algorithm
Intro to Computer Science (MCS 260)
generators and recursion
L-41 22 April 2016 2 / 36
the Fibonacci numbers
The Fibonacci numbers are the sequence
0, 1, 1, 2, 3, 5, 8, . . .
where the next number in the sequence is the sum of the previous two numbers in the sequence. Suppose we have a function:
def fib(k): """ Computes the k-th Fibonacci number. """
and we want to use it to compute the first 10 Fibonacci numbers.
Intro to Computer Science (MCS 260)
generators and recursion
L-41 22 April 2016 3 / 36
the function fib
def fib(k): """ Computes the k-th Fibonacci number. """ if k == 0: return 0 elif k == 1: return 1 else: (prevnum, nextnum) = (0, 1) for i in range(1, k): (prevnum, nextnum) = (nextnum, \ prevnum + nextnum) return nextnum
Intro to Computer Science (MCS 260)
generators and recursion
L-41 22 April 2016 4 / 36
the main program
def main(): """ Prompts the user for a number n and prints the first n Fibonacci numbers. """ nbr = int(input('give a natural number n : ')) fibnums = [fib(i) for i in range(nbr)] print(fibnums)
Running at the command prompt $
$ python fibnum.py give a natural number n : 10 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Problem: with each call to fib, we recompute too much.
Intro to Computer Science (MCS 260)
generators and recursion
L-41 22 April 2016 5 / 36
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- automatic voltage regulator r610
- virtual memory and address translation
- series 2400 sourcemeter user s manual
- chapter 3 pseudo random numbers generators
- automatic voltage regulator r610 instruction manual
- part 1 physical layer simplified specification ver2 00 060925
- resumes and cover letters harvard university
- the ascii character set
- generators recursion and fractals
- capl scripting quickstart vector informatik
Related searches
- c recursion reverse string
- python recursion examples
- c recursion example
- reverse string recursion java
- python recursion limit
- recursion in python
- simple recursion examples
- recursion in c programming
- max recursion depth python
- python increase recursion depth
- when to use recursion programming
- python set recursion depth