introduction to computation and programming using python%2c revised - guttag%2c john v..64.pdf

1
Chapter 4. Functions, Scoping, and Abstraction 47 Figure 4.7 Recursive implementation of Fibonacci sequence Writing the code is the easy part of solving this problem. Once we went from the vague statement of a problem about bunnies to a set of recursive equations, the code almost wrote itself. Finding some kind of abstract way to express a solution to the problem at hand is very often the hardest step in building a useful program. We will talk much more about this later in the book. As you might guess, this is not a perfect model for the growth of rabbit populations in the wild. In 1859, Thomas Austin, an Australian farmer, imported twenty-four rabbits from England, to be used as targets in hunts. Ten years later, approximately two million rabbits were shot or trapped each year in Australia, with no noticeable impact on the population. That’s a lot of rabbits, but not anywhere close to the 120 th Fibonacci number. 24 Though the Fibonacci sequence 25 does not actually provide a perfect model of the growth of rabbit populations, it does have many interesting mathematical properties. Fibonacci numbers are also quite common in nature. 26 Finger exercise: When the implementation of fib in Figure 4.7 is used to compute fib(5), how many times does it compute the value fib(2)? 24 The damage done by the descendants of those twenty-four cute bunnies has been estimated to be $600 million per year, and they are in the process of eating many native plants into extinction. 25 That we call this a Fibonacci sequence is an example of a Eurocentric interpretation of history. Fibonacci’s great contribution to European mathematics was his book Liber Abaci, which introduced to European mathematicians many concepts already well known to Indian and Arabic scholars. These concepts included Hindu-Arabic numerals and the decimal system. What we today call the Fibonacci sequence was taken from the work of the Sanskrit mathematician Pingala. 26 If you are feeling especially geeky, try writing a Fibonacci poem. This is a form of poetry in which the number of syllables in each line is equal to the total number of syllables in the previous two lines. Think of the first line (which has zero syllables) as a place to take a deep breath before starting to read your poem. def fib(n): """Assumes n an int >= 0 Returns Fibonacci of n""" if n == 0 or n == 1: return 1 else: return fib(n-1) + fib(n-2) def testFib(n): for i in range(n+1): print 'fib of', i, '=', fib(i)

Upload: zhichaowang

Post on 09-Sep-2015

236 views

Category:

Documents


1 download

TRANSCRIPT

  • Chapter 4. Functions, Scoping, and Abstraction 47

    Figure 4.7 Recursive implementation of Fibonacci sequence

    Writing the code is the easy part of solving this problem. Once we went from the vague statement of a problem about bunnies to a set of recursive equations, the code almost wrote itself. Finding some kind of abstract way to express a solution to the problem at hand is very often the hardest step in building a useful program. We will talk much more about this later in the book. As you might guess, this is not a perfect model for the growth of rabbit populations in the wild. In 1859, Thomas Austin, an Australian farmer, imported twenty-four rabbits from England, to be used as targets in hunts. Ten years later, approximately two million rabbits were shot or trapped each year in Australia, with no noticeable impact on the population. Thats a lot of rabbits, but not anywhere close to the 120th Fibonacci number.24

    Though the Fibonacci sequence25 does not actually provide a perfect model of the growth of rabbit populations, it does have many interesting mathematical properties. Fibonacci numbers are also quite common in nature.26

    Finger exercise: When the implementation of fib in Figure 4.7 is used to compute fib(5), how many times does it compute the value fib(2)?

    24 The damage done by the descendants of those twenty-four cute bunnies has been estimated to be $600 million per year, and they are in the process of eating many native plants into extinction.

    25 That we call this a Fibonacci sequence is an example of a Eurocentric interpretation of history. Fibonaccis great contribution to European mathematics was his book Liber Abaci, which introduced to European mathematicians many concepts already well known to Indian and Arabic scholars. These concepts included Hindu-Arabic numerals and the decimal system. What we today call the Fibonacci sequence was taken from the work of the Sanskrit mathematician Pingala.

    26 If you are feeling especially geeky, try writing a Fibonacci poem. This is a form of poetry in which the number of syllables in each line is equal to the total number of syllables in the previous two lines. Think of the first line (which has zero syllables) as a place to take a deep breath before starting to read your poem.

    def fib(n): """Assumes n an int >= 0 Returns Fibonacci of n""" if n == 0 or n == 1: return 1 else: return fib(n-1) + fib(n-2) def testFib(n): for i in range(n+1): print 'fib of', i, '=', fib(i)