Computer Science: An Overview (13th Edition) (What's New in Computer Science)
13th Edition
ISBN: 9780134875460
Author: Glenn Brookshear, Dennis Brylow
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 5.5, Problem 3QE
What sequence of numbers would be printed by the following recursive function if we started it with N assigned the value 1?
def Exercise (N):
print (N)
if (N < 3) :
Exercise (N + 1)
Print (N)
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Consider the recursive procedure which computes the nth Fibonacci number is the one below.
procedure Fl (n)
//a function which returns the nth Fibonacci number.//
if n < 2 then return(n)
else return (F2(2,n,1,1))
endif
end Fl
procedure F2(i,n,x,y)
if i
is it correct ?
Write a recursive function that returns the nth number in a fibonacci sequence when n is passed to function. The fibonacci sequence is like 0,1,1,2,3,5,8,13......
Answer:
#include <iostream>
using namespace std;
int getFibonacci(int n) {
if (n == 0 || n == 1)
return n;
else
return getFibonacci(n - 1) + getFibonacci(n - 2);
}
int main() {
int n = 7;
int result = getFibonacci(n);
cout << result;
}
Which is the base case of the following recursion function:
def mult3(n): if n == 1: return 3 else: return mult3(n-1) + 3
else
n == 1
mult3(n)
return mult3(n-1) + 3
Chapter 5 Solutions
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Ch. 5.1 - Prob. 1QECh. 5.1 - Prob. 2QECh. 5.1 - Prob. 3QECh. 5.1 - Suppose the insertion sort as presented in Figure...Ch. 5.2 - A primitive in one context might turn out to be a...Ch. 5.2 - Prob. 2QECh. 5.2 - The Euclidean algorithm finds the greatest common...Ch. 5.2 - Describe a collection of primitives that are used...Ch. 5.3 - Prob. 2QECh. 5.3 - Prob. 3QE
Ch. 5.3 - Prob. 4QECh. 5.4 - Modify the sequential search function in Figure...Ch. 5.4 - Prob. 2QECh. 5.4 - Some of the popular programming languages today...Ch. 5.4 - Suppose the insertion sort as presented in Figure...Ch. 5.4 - Prob. 5QECh. 5.4 - Prob. 6QECh. 5.4 - Prob. 7QECh. 5.5 - What names are interrogated by the binary search...Ch. 5.5 - Prob. 2QECh. 5.5 - What sequence of numbers would be printed by the...Ch. 5.5 - What is the termination condition in the recursive...Ch. 5.6 - Prob. 1QECh. 5.6 - Give an example of an algorithm in each of the...Ch. 5.6 - List the classes (n2), (log2n), (n), and (n3) in...Ch. 5.6 - Prob. 4QECh. 5.6 - Prob. 5QECh. 5.6 - Prob. 6QECh. 5.6 - Prob. 7QECh. 5.6 - Suppose that both a program and the hardware that...Ch. 5 - Prob. 1CRPCh. 5 - Prob. 2CRPCh. 5 - Prob. 3CRPCh. 5 - Select a subject with which you are familiar and...Ch. 5 - Does the following program represent an algorithm...Ch. 5 - Prob. 6CRPCh. 5 - Prob. 7CRPCh. 5 - Prob. 8CRPCh. 5 - What must be done to translate a posttest loop...Ch. 5 - Design an algorithm that when given an arrangement...Ch. 5 - Prob. 11CRPCh. 5 - Design an algorithm for determining the day of the...Ch. 5 - What is the difference between a formal...Ch. 5 - Prob. 14CRPCh. 5 - Prob. 15CRPCh. 5 - The following is a multiplication problem in...Ch. 5 - Prob. 17CRPCh. 5 - Four prospectors with only one lantern must walk...Ch. 5 - Starting with a large wine glass and a small wine...Ch. 5 - Two bees, named Romeo and Juliet, live in...Ch. 5 - What letters are interrogated by the binary search...Ch. 5 - The following algorithm is designed to print the...Ch. 5 - What sequence of numbers is printed by the...Ch. 5 - Prob. 24CRPCh. 5 - What letters are interrogated by the binary search...Ch. 5 - Prob. 26CRPCh. 5 - Identity the termination condition in each of the...Ch. 5 - Identity the body of the following loop structure...Ch. 5 - Prob. 29CRPCh. 5 - Design a recursive version of the Euclidean...Ch. 5 - Prob. 31CRPCh. 5 - Identify the important constituents of the control...Ch. 5 - Identify the termination condition in the...Ch. 5 - Call the function MysteryPrint (defined below)...Ch. 5 - Prob. 35CRPCh. 5 - Prob. 36CRPCh. 5 - Prob. 37CRPCh. 5 - The factorial of 0 is defined to be 1. The...Ch. 5 - a. Suppose you must sort a list of five names, and...Ch. 5 - The puzzle called the Towers of Hanoi consists of...Ch. 5 - Prob. 41CRPCh. 5 - Develop two algorithms, one based on a loop...Ch. 5 - Design an algorithm to find the square root of a...Ch. 5 - Prob. 44CRPCh. 5 - Prob. 45CRPCh. 5 - Design an algorithm that, given a list of five or...Ch. 5 - Prob. 47CRPCh. 5 - Prob. 48CRPCh. 5 - Prob. 49CRPCh. 5 - Prob. 50CRPCh. 5 - Prob. 51CRPCh. 5 - Does the loop in the following routine terminate?...Ch. 5 - Prob. 53CRPCh. 5 - Prob. 54CRPCh. 5 - The following program segment is designed to find...Ch. 5 - a. Identity the preconditions for the sequential...Ch. 5 - Prob. 57CRPCh. 5 - Prob. 1SICh. 5 - Prob. 2SICh. 5 - Prob. 3SICh. 5 - Prob. 4SICh. 5 - Prob. 5SICh. 5 - Is it ethical to design an algorithm for...Ch. 5 - Prob. 7SICh. 5 - Prob. 8SI
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Show the output of the following program: public class Test { public static void main(String[] args) { String s...
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Consider the adage Never ask a question for which you do not want the answer. a. Is following that adage ethica...
Experiencing MIS
In Exercises 39 through 44, write a program to carry out the task. A form contains two text boxes and one large...
Introduction To Programming Using Visual Basic (11th Edition)
Write a program to print the corresponding Celsius to Fahrenheit table.
C Programming Language
Answer question 3.33, but do not consider any pet having the breed of Unknown.
Database Concepts (8th Edition)
A file that contains a Flash animation uses the __________ file extension. a. .class b. .swf c. .mp3 d. .flash
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
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
- In this problem, you will write different programs to x^N, where x ∈ R, n ∈ N. b) Devise a recursive algorithm to compute x^n , using the fact that x^n= x ⋅ x^n−1 .arrow_forwardThis is in Python This has two parts. This is a problem on recursion. Part a: Write a recursive function that accepts an integer argument, n. The user needs to be asked for the number n. The function should display n lines of asterisks on the screen, with the first (the top) showing 1 asterisk, the second from the top showing two asterisks, up to the nth line which shows n asterisks. Part b. Write a recursive function that accepts an integer argument, n. The user needs to be asked for the number n. The function should display n lines of asterisks on the screen, with the first (the top) showing n asterisks, the second from the top showing n-1 asterisks, up to the nth line which shows 1 asterisk. Submit the two files and two sample outputs for each of the parts.arrow_forward6) Write a recursive function to calculate the summation of numbers from 1 to n. For exampleif the user enters 5, your program would add 1+2+3+4+5 and print out the answer 15.7) Write a recursive program to calculate Fibonacci numbers. Use the definition of a Fibonaccinumber where F(n) = F(n-1) + F(n-2). use java to codearrow_forward
- What type of recursion is used in the following function? int f(int n){ if (n==1) return 1; else return n+f(n-1); } Tail recursion Multiple recursion Indirect recursion Non-tail recursionarrow_forwardWrite a program using a user defined recursive function to implement the following code segment. main() will call the recursive function and print the value of sum.int n, i, sum;n= last_two_digits_of_your_student_id+3;sum=0;for (i=n-3; i<=n; i++){sum=sum+i;}printf(“%d”, sum)arrow_forwardWrite a recursive function to convert a given string into the number it represents. That is input will be a numeric string that contains only numbers, you need to convert the string into corresponding integer and return the answer. Input format : Numeric string S (string, Eg. "1234") Output format : Corresponding integer N (int, Eg. 1234) Constraints : 0 <= |S| <= 9 where |S| represents length of string S. Sample Input 1 : 00001231 Sample Output 1 : 1231 Sample Input 2 : 12567 Sample Output 2 : 12567 Solution:////////////////.arrow_forward
- PYTHON!!! Write a function sum_of_digits(a,b), that takes two arguments a and b, computes a to the power of b, then recursively finds the sum of digits until there is only one digit left. You must PRINT all steps of the process as shown in the below example. sum_of_digits(5,3) will PRINT on the screen the following: 5^3 = 125 = 1 + 2 + 5 = 8 sum_of_digits(2,8) will PRINT on the screen the following: 2^8 = 256 = 2 + 5 + 6 = 13 = 1 + 3 = 4arrow_forwardFor function addOdd(n) write the missing recursive call. This function should return the sum of all postive odd numbers less than or equal to n. Examples: addOdd(1) -> 1addOdd(2) -> 1addOdd(3) -> 4addOdd(7) -> 16 public int addOdd(int n) { if (n <= 0) { return 0; } if (n % 2 != 0) { // Odd value return <<Missing a Recursive call>> } else { // Even value return addOdd(n - 1); }}arrow_forwardThe task is to get input (n) and make a list from 2 to n and remove its multiples from the list by applying recursive functions. Example: Input: 6 Output: [2, 3, 4, 5, 6] How to apply this python code in the task? def createList(n): #Base Case/s #TODO: Add conditions here for your base case/s #if <condition> : #return <value> #Recursive Case/s #TODO: Add conditions here for your recursive case/s #else: #return <operation and recursive call> #remove the line after this once you've completed all the TODO for this function return [] def removeMultiples(x, arr): #Base Case/s #TODO: Add conditions here for your base case/s #if <condition> : #return <value> #Recursive Case/s #TODO: Add conditions here for your recursive case/s #else: #return <operation and recursive call> #remove the line after this once you've completed all the TODO for this function return []arrow_forward
- The Fibonacci algorithm is a famous mathematical function that allows us to create a sequence of numbers by adding together the two previous values. For example, we have the sequence:1, 1, 2, 3, 5, 8, 13, 21…Write your own recursive code to calculate the nth term in the sequence. You should accept a positive integer as an input, and output the nth term of the sequence.Once you have created your code, add comments describing how the code works, and the complexity of any code you have created.arrow_forwardPYTHON!!! Write a function to compute the following recursive value: A(n) = A(n-1) + 1 , if n is odd A(n-1) + 2 , if n is even 0 , if n is 1 """ def recursion(n): PYTHONarrow_forwardIn java Reprogram the following iterative function with recursion. int f(int N) { int count, sum = 0; for (count = 0; count <= N; count++) sum = sum + count; return sum; }arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Computational Software for Intelligent System Design; Author: Cadence Design Systems;https://www.youtube.com/watch?v=dLXZ6bM--j0;License: Standard Youtube License