Recursion exercise

.docx

School

Northeastern University *

*We aren’t endorsed by this school

Course

1285

Subject

Computer Science

Date

May 7, 2024

Type

docx

Pages

4

Uploaded by AmbassadorSnow13278 on coursehero.com

Introduction This activity explores recursive way to solve problems. To solve a large problem, we use the solution of a smaller but similar problem. Eventually, we reach the most basic problem, for which we know the answer. Complete the worksheet to the best of your ability. You’re encourage to work with partner(s) but each of you should be filing your own worksheet. If you get stuck with some question, move on to the next question – maybe this will shed the light on the previous question too. Part 1 Factorial: Critical Thinking Questions 1. Consider two different ways to show how to calculate 4! (the product of the numbers from 1 to 4). a) Write out all numbers that explicitly needed to be multiplied 4! = 1 * 2 * 3 * 4 b) Write the expression using 3! 4! = 3! * 4 2. Write an expression similar to question 1 showing how each factorial can be calculated in terms of a “simpler” factorial. (By definition, 0! equals 1.) a) 3! = 2! * 3 b) 2! = 1! * 2 c) 1! = 0! * 1 d) 100! = 99! * 100 3. Generalize your group’s answer to question 2 in terms of n : write down a formula to compute n! using factorial of a smaller number. n! = (n-1) * n 1
4. When faced with a large problem to solve, we might seek to use a solution to a smaller, simpler problem. If we repeatedly decompose the original problem into smaller simpler expressions, we will eventually identify the most simple or basic component that can’t be broken down any further. This is referred to as the base case . What would your group propose to be the base case for factorial computation? I.e. what is the smallest number for which factorial can be easily computed (and cannot be represented by the above formula)? The base case is when we calculate the 0! Since that is equal to 1 as mentioned above Part 2 Analyzing recursive code When a function makes a call to itself this is referred to as recursion . To define a recursive function, you should write an if-statement that checks for the base case. When the operation is not the base case, you include a call to the function you are writing (known as the recursive call ). Consider the following code for factorial computation: ’’’calculates n factorial’’’ def factorial(n): if n == 0: return 1 else: print(n) return n * factorial(n-1) Critical Thinking Questions 1. Underline the recursive call in the above code. 2. What happens if you try to calculate the factorial of a negative number? Fix the bug in the factorial method so this does not occur. It would go on forever so you should add a condition that breaks out the loop if n < 0 If n< 0: print(“Cannot find the factorial of negative numbers”) break 3. How many distinct calls are made to the factorial() function to calculate the factorial of 3? Identify the value of the parameter n for each of these separate calls. 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help