Question

1. The following code recursively calculates the maximum value in a list. def recursive_max_impl( L, i): The actual recursive function. if i == len(L)-1: return L[i] else: return max(L[i], recursive_max_impl(L,i+1 def recursive_max (L): The driver for the recursive function. This case of an empty list and otherwise makes the recursive function. if len(L) == 0: return -99999 # By convention else: return recursive_max_impl(L, 0) if __name__ == "__main__": L1 = [5] print(recursive_max (L1)) L2 = [ 24, 23.1, 12, 15, 1] print(recursive_max(L2)) L2.append(55) print(recursive_max(L2)) Using this as a guide, write a recursive function to add the values in a list. You should have to change very little code. Implement your code in the provided file prob1.py 2. Implement a recursive solution to the Fibonacci number function definition given during lecture. Implement your code in the provided file prob2.py. 3. Looking carefully at the Fibonacci definition shows that calculating Fibonacci number $f_{n-1}$ requires calculating Fibonacci number $f_{n-2}$, which is also required for calculating Fibonacci number $f_n$. This means there is redundant computation. This redundancy gets worse for numbers $f_{n-3}$, $f_{n-4}$, etc. Fortunately, the Fibonacci sequence is relatively

          1. The following code recursively calculates the
maximum value in a list.
def recursive_max_impl( L, i):
The actual recursive function.
if i == len(L)-1:
return L[i]
else:
return max(L[i], recursive_max_impl(L,i+1
def recursive_max (L):
The driver for the recursive function. This
case of an empty list and otherwise makes the
recursive function.
if len(L) == 0:
return -99999 # By convention
else:
return recursive_max_impl(L, 0)
if __name__ == "__main__":
L1 = [5]
print(recursive_max (L1))
L2 = [ 24, 23.1, 12, 15, 1]
print(recursive_max(L2))
L2.append(55)
print(recursive_max(L2))
Using this as a guide, write a recursive function to
add the values in a list. You should have to change
very little code. Implement your code in the
provided file prob1.py
2. Implement a recursive solution to the Fibonacci
number function definition given during lecture.
Implement your code in the provided file
prob2.py.
3. Looking carefully at the Fibonacci definition
shows that calculating Fibonacci number $f_{n-1}$
requires calculating Fibonacci number $f_{n-2}$, which
is also required for calculating Fibonacci number
$f_n$. This means there is redundant computation.
This redundancy gets worse for numbers $f_{n-3}$,
$f_{n-4}$, etc.
Fortunately, the Fibonacci sequence is relatively
        
Show more…
1. The following code recursively calculates the
maximum value in a list.
def recursivemaximpl( L, i):
The actual recursive function.
if i == len(L)-1:
return L[i]
else:
return max(L[i], recursivemaximpl(L,i+1
def recursivemax (L):
The driver for the recursive function. This
case of an empty list and otherwise makes the
recursive function.
if len(L) == 0:
return -99999 # By convention
else:
return recursivemaximpl(L, 0)
if name == "main":
L1 = [5]
print(recursivemax (L1))
L2 = [ 24, 23.1, 12, 15, 1]
print(recursivemax(L2))
L2.append(55)
print(recursivemax(L2))
Using this as a guide, write a recursive function to
add the values in a list. You should have to change
very little code. Implement your code in the
provided file prob1.py
2. Implement a recursive solution to the Fibonacci
number function definition given during lecture.
Implement your code in the provided file
prob2.py.
3. Looking carefully at the Fibonacci definition
shows that calculating Fibonacci number fn-1
requires calculating Fibonacci number fn-2, which
is also required for calculating Fibonacci number
fn. This means there is redundant computation.
This redundancy gets worse for numbers fn-3,
fn-4, etc.
Fortunately, the Fibonacci sequence is relatively

Added by Tammy R.

Close

Computer Science and Information Technology
Computer Science and Information Technology
Trishna Knowledge Systems 2018 Edition
AceChat toggle button
Close icon
Ace pointing down

Please give Ace some feedback

Your feedback will help us improve your experience

Thumb up icon Thumb down icon
Thanks for your feedback!
Profile picture
Question 3) Looking carefully at the Fibonacci definition shows that calculating Fibonacci number fn-1 requires calculating Fibonacci number fn-2, which is also required for calculating Fibonacci number fn. This means there is redundant computation. This redundancy gets worse for numbers fn-3, fn-4, etc. Fortunately, the Fibonacci sequence is relatively easy to compute non-recursively. That is your problem here. The trick is to build up the solution using a for loop that calculates f2, then f3, then f4, etc. Implement your solution in prob3.py. 1. The following code recursively calculates the maximum value in a list. Jef recursive max_impl(L, 1): The actual recursive function. == len(L)-l: return L[i] else: return Max(Llil, recursive max_impl(l,i+l Jef recursive max(L): The driver for the recursive function. This case empty list and otherwise makes the recursive function if len(L) return ~99999 By convention else: return recursive_Max impl( name ~_Main [5 print(recursive max(Li)) 24, 23.1, 12, 15, 1] print(recursive_max(L2)) L2.append(print(recursive_max(L2)) Using this as a guide, write recursive function to add the values in a list. You should have to change very little code: Implement your code in the provided file probl.Py 2. Implement recursive solution to the Fibonacci number function definition given during lecture: Implement your code in the provided file prob2.Py 3. Looking carefully at the Fibonacci definition shows that calculating Fibonacci number fn-1 requires calculating Fibonacci number fn-2, which is also required for calculating Fibonacci number fn. This means there is redundant computation. This redundancy gets worse for numbers fn-3, fn-4 etc. Fortunately, the Fibonacci sequence is relatively easy to compute non-recursively.
Close icon
Play audio
Feedback
Powered by NumerAI
David Collins Ivan Kochetkov
Jennifer Stoner verified

Adi S and 93 other subject AP CS educators are ready to help you.

Ask a new question

*

Labs

-

Want to see this concept in action?

NEW

Explore this concept interactively to see how it behaves as you change inputs.

View Labs

*

Key Concepts

-
Key Concept
Premium Feature
Explore the core concept behind this problem.
Play button
Key Concept
Premium Feature
Explore the core concept behind this problem.
Your browser does not support the video tag.

*

Recommended Videos

-
the-fibonacci-sequence-is-a-well-known-sequence-in-mathematics-that-is-obtained-following-simple-rule-set-the-first-two-entries-of-the-sequence-to-be-0-and-1-and-then-inductively-find-the-ne-91729

The Fibonacci sequence is a well known sequence in mathematics that is obtained following a simple rule: set the first two entries of the sequence to be 0 and 1, and then, inductively, find the next entry by adding two previous entries. This results in the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . . It is natural to ask whether one can find a formula for the nth Fibonacci number (i.e., the nth entry of this sequence) without having to compute all previous entries. Such rules are called recursions and in this problem we will develop a method to analyze recursion relations. (a) Set F_0 = 0, F_1 = 1, and let F_n denote the nth Fibonacci number (with the convention that 0 is the 0th Fibonacci number). Then the sequence of Fibonacci numbers can be obtained by running the recursion F_n = F_{n-1} + F_{n-2}; F_0 = 0, F_1 = 1. Now, set v_n = [F_n, F_{n-1}]^T. Identify a matrix A such that the recursion above can be written, equivalently, as v_{n+1} = A v_n; v_0 = [F_1, F_0]^T. (b) Using the matrix recursion above A, obtain a (non-recursive) formula for v_{n+1} in terms of powers of A and v_0. (c) Let lambda_1 = (1 + sqrt{5})/2 and lambda_2 = (1 - sqrt{5})/2. Diagonalize A and give a formula for each entry of A^n in terms of lambda_1 and lambda_2. (d) Use your answer from the previous part to obtain a formula for F_n in terms of lambda_1 and lambda_2. (e) Show that lim_{n oinfty} F_{n+1}/F_n = lambda_1. (Note that lambda_1 is the famous "golden ratio").

Adi S.

fibonacci-sequence-problem-these-numbers-form-the-fibonacci-seque-nce-11235813213455-dots-picture-ca

Fibonacci Sequence Problem: These numbers form the Fibonacci seque nce: $1,1,2,3,5,8,13,21,34,55, \dots$ PICTURE CANT COPY a. Figure out the recursion pattern followed by these Fibonacci numbers. Write the next two terms of the sequence. Enter the recursion formula into your grapher. You will need to enter $u(n \mathrm{Min})=\{1,1\}$ to show that the first two terms are given. Make a table of Fibonacci numbers and scroll down to find the 20 th term of the sequence. b. Find the first ten ratios, $r_{m}$, of the Fibonacci numbers, where $$r_{n}=\frac{t_{n+1}}{t_{n}}$$ Show that these ratios get closer and closer to the golden ratio, $$r=\frac{\sqrt{5}+1}{2}=1.61803398 \ldots$$ c. Find a pinecone, a pineapple, or a sunflower, or a picture of one of these. Each has sections formed by intersections of two spirals, one in one direction and another in the opposite direction. Count the number of spirals in each direction. What do you notice about these numbers? PICTURE CANT COPY d. Look up Leonardo Fibonacci (also known as Leonardo of Pisa) on the Internet or via another reference source. Find out when and where he lived. See if you can find out how he related the sequence to the growth of a population of rabbits, and why, therefore, his name is attached to the sequence.

Precalculus with Trigonometry: Concepts and Applications

Sequences and Series

Arithmetic, Geometric, and Other Sequences

question-5-consider-the-while-loop-below-that-computes-all-fibonacci-numbers-less-than-500-fibl-and-fibz-will-represent-the-two-latest-terms-in-the-sequence-fibl-ini-ticlize-fibl-fib2-initic-72225

Consider the while() loop below that computes all Fibonacci numbers less than 500. # fib1 and fib2 will represent the two latest terms in the sequence. fib1 <- 1 # Initialize fib1 fib2 <- 1 # Initialize fib2 # Create the vector to store the output from the while loop. full.fib <- c(fib1,fib2) # While the sum of the last two terms is less than 500, execute the following commands. while(fib1 + fib2 < 500){ # Save the latest term to old.fib2. old.fib2 <- fib2 # Compute the sum of the latest two terms and assign the sum to be the new latest term. fib2 <- fib1 + fib2 # Append the latest term to the end of the full.fib vector with all previous terms. full.fib <- c(full.fib,fib2) # Save the previously latest term (now the second to last term) to fib1. fib1 <- old.fib2 } # Print the output from the while loop. full.fib (a) The variable old.fib2 is not actually necessary. Rewrite the while() loop with the update of fib1 based on just the current values of fib1 and fib2. (b) In fact, fib1 and fib2 are not necessary either. Rewrite the while() loop without using any variables except full.fib. (c) Determine the number of Fibonacci numbers less than 1000000.

Sri K.


*

Recommended Textbooks

-
Computer Science and Information Technology

Computer Science and Information Technology

Trishna Knowledge Systems 2018 Edition
achievement 1,890 solutions
Introduction to Programming Using Python

Introduction to Programming Using Python

Y. Daniel Liang 1st Edition
achievement 1,143 solutions
Computer Science - An Overview

Computer Science - An Overview

Glenn Brookshear, Dennis Brylow 12th Edition
achievement 1,873 solutions

*

Transcript

-
00:02 Given that vn is equal to fn and fn -1 to the power t and vn plus 1 is equal to a into vn and v not is equal to f1 comma f not to the power t that is 1 comma 0 to the power t...
Need help? Use Ace
Ace is your personal tutor. It breaks down any question with clear steps so you can learn.
Start Using Ace
Ace is your personal tutor for learning
Step-by-step explanations
Instant summaries
Summarize YouTube videos
Understand textbook images or PDFs
Study tools like quizzes and flashcards
Listen to your notes as a podcast
Continue solving this problem
Create a free account to:
  • View full step-by-step solution
  • Ask follow-up questions with Ace AI
  • Save progress and study later
Continue Free
Numerade

Get step-by-step video solution
from top educators

Continue with Clever
or



By creating an account, you agree to the Terms of Service and Privacy Policy
Already have an account? Log In

A free answer
just for you

Watch the video solution with this free unlock.

Numerade

Log in to watch this video
...and 100,000,000 more!


EMAIL

PASSWORD

OR
Continue with Clever