THREE QUESTIONS - USE PYTHON LANGUAGE 8. Use the Design Recipe to define a funct
ID: 3729253 • Letter: T
Question
THREE QUESTIONS - USE PYTHON LANGUAGE
8. Use the Design Recipe to define a function called sumPerfectSquare that accepts a variable num and returns the sum of the perfect squares less than or equal to num.
Hint: Use a while loop which and an accumulator variable.
For example, if num is 10, then the function should return 14
Since the perfect squares that are less than or equal to 10 are: 1 4 9
1 + 4 + 9 = 14
9. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:
Fn = Fn-1 + Fn-2
F1 = 1
F0 = 1
The first 12 Fibonacci numbers follow:
1 , 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.
Use the Design Recipe to write a function called fib which consumes n and returns the nth Fibonacci number. Use accumulation variables to implement your fib function. Hint: you will need three accumulation variables to keep track of Fn and Fn-1 and Fn-2.
You should create special cases for n of 0 or 1 and then use a for loop over the range 2 through n to compute the nth term.
For example:
10. Given the following code:
Write code that prints the result of calling the test function with each of the interesting 6 combinations of cases. Your code should end up printing:
Note: you should have exactly 6 lines of code in your submission, each one of the form
with values in the blanks.
The quiz server will match your output to the above AND print out of how many of the 6 interesting cases your tests covered.
Test Result print(fib(10)) 89Explanation / Answer
8.
9.
10.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.