Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CptS 111 — Homework #5 Due Friday, Mar. 7, 2014 F UN WITH for-L OOPS AND F IBONACCI This homework assignment isn’t nearly as long as it looks, so don’t panic! There’s just a lot of information to pass along before you can start the actual assignment. Header information. Your program must include the following information in the header. • • • • CptS 111, HW #5 Original author and date Name of the module Brief description and/or purpose of the module Program: Fibonacci Sequence Redux In Lab #6, you calculated several terms in the Fibonacci sequence. In this homework you’re going to do a bit more and also do it in a much less redundant fashion. As a reminder, here’s the background information I provided together with a description of the task that was assigned in the lab: Believe it or not, the golden ratio has been used to predict trends in the stock market and for other applications in financial analysis. The golden ratio and the Fibonacci sequence are related, and both seem to reflect a basic law of nature which you can read about here. The relationship is given by lim n→∞ F (n + 1) =φ F (n) where F (n) is the nth term in the Fibonacci sequence and φ is the golden ratio given by √ 1+ 5 φ= 2 In other words, for large values of n, you can find the approximate value of the next term in the Fibonacci sequence by multiplying the current term by the golden ratio. You may recall that the exact Fibonacci sequence is obtained by adding two successive terms to get the next term: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ... i.e., 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, and so on. In this task you’ll determine how well the approximation works by calculating several successive terms of the Fibonacci sequence, calculating their approximate values, finding the difference between them, and displaying all three sets of numbers. Also, I can’t resist including an image of the Fibonacci spiral here: 1 This spiral is constructed by representing each term of the Fibonacci sequence by a square with sides equal to the value of the term. First you draw two squares with sides of length 1 next to each other; you add a square with sides of length 2 adjacent to the original two squares; next you add a square with sides of length 3 adajacent to the three existing squares, and so on. After you have as many boxes as you wish, you draw quarter-circle arcs through each adjacent square and voila! Approximations of Fibonacci spirals show up in nature all the time as seen in the next two images of a snail shell and Hurricane Irene: We’re going to invert things a bit from the lab task. Rather than using the golden ratio φ to approximate terms of the Fibonacci sequence, we’re going to approximate the golden ratio using successive terms of the Fibonacci sequence. To begin we must first convert the sequence shown above into a mathematical formula. We write: xn+1 = xn + xn−1 (1) Don’t be intimidated by this formula. It simply states that the “next” value in the sequence relative to the nth term is the sum of the nth term and the n − 1th term. We can “seed” the sequence by starting with the first two terms to obtain the third: x2 = x1 + x0 2 = 1+1 2 How do you write an algorithm to implement Eq. (1) and then generate a given number of terms in the Fibonacci sequence? Imagine there’s a window just wide enough for you to see spaces for three terms in the sequence as shown in the figure below. When you align this window with the three spaces, the first and second numbers are known, and it’s easy to determine the third number: simply add the first and second numbers. Next push the window to the right one space so that the new first number previously was the second number and the new second number previously was the third. Once again the first and second numbers in the window are known. Add them to obtain the third number. You can repeat this to determine as many number of terms as you want. first second third 1, 1, ?, ... first second third 1, 1, 2, ?, ... first second third 1, 1, 2, 3, ?, ... Next, consider the following code. It prints the first 10 numbers of the Fibonacci sequence—11 with the starting value x0 . 1 2 3 4 5 6 7 8 9 # seed the starting values first = 1 print(first) second = 1 for i in range(10): print(second) third = first + second first = second second = third # # # # # # # # x_0 print the starting value, x_0 x_1 execute loop 10 times print current ’second’ value set third to the next number in the series set first to previous ’second’ value set second to previous ’third’ value The window we imagined above is represented by three variables: first, second, and third. We set first and second to one. After we’ve initialized first, we print its value before entering the for-loop. In the for-loop, the value of second is printed, and the next number in the sequence is calculated and assigned to the variable third. Next the first and second variables are reset, 3 which is just like moving our window to the right one space. (This can be coded more pythonically using simultaneous assignment.) Now look at the code and the window figure and make sure you understand both before continuing on to the actual programming assignment! For this assignment you must write a single program with two functions. You don’t need to include an html file this time, but you do need to include docstrings. The two functions are: • fib(): Takes a single integer argument numTerms greater than or equal to 1 and for each of the numTerms terms of the Fibonacci sequence prints (1) the number of the term—i.e., 1, 2, 3..., (2) the corresponding Fibonacci number, and (3) the ratio of the current and previous Fibonacci numbers as shown below. The ratio, printed only for x1 and higher, is an approximation of the golden ratio φ = 1.61803398875... • main(): Prompts the user for the number of terms s/he wants to find and calls fib(). 1 2 3 1 2 3 4 5 6 7 8 9 10 Enter the number of terms you want to find: 2 1 1 2 1 1.0 Enter the number of terms you want to find: 9 1 1 2 1 1.0 3 2 2.0 4 3 1.5 5 5 1.66666666667 6 8 1.6 7 13 1.625 8 21 1.61538461538 9 34 1.61904761905 The last line of your program should be a call to main(). Don’t forget to include docstrings! How many terms are needed to obtain an approximation to the golden ratio accurate to 4 decimal places? Submission information. Create a directory called hw5, put your program under this directory, zip the directory as you did for your previous assignments, and submit it at our class Web site. 4