Starting Out with Python (3rd Edition)
3rd Edition
ISBN: 9780133582734
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 12, Problem 2SA
In this chapter, the rules given for calculating the factorial of a number are as follows:
If n = 0 then factorial (n) = 1
If n . 0 then factorial (n) = n × factorial (n - 1)
If you were designing a function from these rules, what would the base case be? What would the recursive case be?
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
In this chapter, the rules given for calculating the factorial of a number are as follows:If n = 0 then factorial(n) = 1If n . 0 then factorial(n) = n × factorial(n – 1)If you were designing a function from these rules, what would the base case be? What wouldthe recursive case be?
Each of the following recursive function definitions contains an error. Briefly but fully and clearly explain what is wrong with each definition.
f(x) = 3*f(x-5) where x is a positive integer.
f(0) = 7
2. g(x) = 2 - 4*g(x) where x is a positive integer.
g(1) = 3
g(2) = 4
The following recursive function takes three positive integer arguments:
def compute(n,x,y) :
if n==0 : return x
return compute(n-1,x+y,y)
What is the value returned by the compute function?
n*x+y
x+y
x+n*y
x
What if: will the returned value be for the compute function defined in the question above if the argument n is negative?
x
x-n*y
x+n*y
The function will never return a value.
Chapter 12 Solutions
Starting Out with Python (3rd Edition)
Ch. 12.2 - It is said that a recursive algorithm has more...Ch. 12.2 - Prob. 2CPCh. 12.2 - What is a recursive case?Ch. 12.2 - What causes a recursive algorithm to stop calling...Ch. 12.2 - What is direct recursion? What is indirect...Ch. 12 - Prob. 1MCCh. 12 - A function is called once from a program's main...Ch. 12 - Prob. 3MCCh. 12 - Prob. 4MCCh. 12 - Prob. 5MC
Ch. 12 - Prob. 6MCCh. 12 - Any problem that can be solved recursively can...Ch. 12 - Actions taken by the computer when a function is...Ch. 12 - A recursive algorithm must _______ in the...Ch. 12 - A recursive algorithm must ______ in the base...Ch. 12 - An algorithm that uses a loop will usually run...Ch. 12 - Some problems can be solved through recursion...Ch. 12 - It is not necessary to have a base case in all...Ch. 12 - In the base case, a recursive method calls itself...Ch. 12 - In Program 12-2 , presented earlier in this...Ch. 12 - In this chapter, the rules given for calculating...Ch. 12 - Is recursion ever required to solve a problem?...Ch. 12 - When recursion is used to solve a problem, why...Ch. 12 - How is a problem usually reduced with a recursive...Ch. 12 - What will the following program display? def...Ch. 12 - Prob. 2AWCh. 12 - The following function uses a loop. Rewrite it as...Ch. 12 - Prob. 1PECh. 12 - Prob. 2PECh. 12 - Prob. 3PECh. 12 - Largest List Item Design a function that accepts a...Ch. 12 - Recursive List Sum Design a function that accepts...Ch. 12 - Prob. 6PECh. 12 - Prob. 7PECh. 12 - Ackermann's Function Ackermann's Function is a...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
For each of the following E-R diagrams from Chapter 2 C: Transform the diagram to a relational schema that show...
Modern Database Management (12th Edition)
Which of the following are correct? a. False |= True. b. True |= False. c. (A B) |= (A B). d. A B |= A B. e...
Artificial Intelligence: A Modern Approach
T F: The multiplication operator has higher precedence than the addition operator.
Starting Out With Visual Basic (7th Edition)
Describe some objects that might be found in a program for simulating the pedestrian traffic in a hotel lobby. ...
Computer Science: An Overview (12th Edition)
Write out the header of a for-each loop to process an ArrayList called tracks. Don't worry about the loop's bod...
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Show the output of the following code: public class Test { public static void main(String[] args) { WeightedGra...
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- The Listed program is about a recursive function named mystery assume the parameter Is never negative and test with several different calls to the function. Explain in sentences what mystery is doing/returning. Explain in higher level what mystery is computingarrow_forwardQuestion 2 1. What is the possible output if this recursive function run if x is 5 and y is 2. static int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } 2. Calculate the big O notation for this function below. T(n) = 3n3 + 2n + 7arrow_forwardConsider the following pseudocode recursive function GUESS(n, m). Trace the code to see what happens when the recursive function is run with n = 6 and m = 3. function GUESS(n, m)if (m > n) then return (0);if (m = n) then return (1);if (n = 1) or (m = 0) then return (1);if (m < n) then return ((n-1)*GUESS(n-1, m-1));end GUESSarrow_forward
- When a recursive function is run, there is an additional demand placed on the memory of the computer and the amount of time it takes to run the programme.arrow_forwardWrite a recursive function that takes a positive integer and returns the factorial of that integer. Attention, the function must be recursive, and its name must be "fatorial".arrow_forwardHow much memory and CPU time are needed to perform recursive functions, according to%?arrow_forward
- Find the logic error(s) in the following recursive function, and explain how to correct them. This function should find the sum of the values from 0 to n. def sum(n): if n == 0: return 0 Else: Return n + sum(n)arrow_forwardPlease can be handwritten. Question 2: Implementing a Recursive Function . Write recursive function, recursionprob(n), which takes a positive number as its argument and returns the output as shown below. The solution should clearly write the steps as shown in an example in slide number 59 and slide number 60 in lecture slides. After writing the steps, trace the function for “recursiveprob(5)” as shown in an example slide number 61. Function Output: >> recursionprob(1) 1 >> recursionprob(2) 1 4 >> recursionprob(3) 1 4 9 >>recrusionprob(4) 1 4 9 16arrow_forwardQuestion 2: Implementing a Recursive Function .Write recursive function, recursionprob(n), which takes a positive number as its argument and returns the output as shown below. The solution should clearly write the steps as shown in an example in slide number 59 and slide number 60 in lecture slides. After writing the steps, trace the function for “recursiveprob(5)” as shown in an example slide number 61. Function Output: >> recursionprob(1) 1 >> recursionprob(2) 1 4 >> recursionprob(3) 1 4 9 >>recrusionprob(4) 1 4 9 16arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Computational Software for Intelligent System Design; Author: Cadence Design Systems;https://www.youtube.com/watch?v=dLXZ6bM--j0;License: Standard Youtube License