Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question
Program #2 Define a sequence of numbers recursively
Define a sequence ao, a1, az, a3, where
ao = 1
an = (an -1+ 1) * 2
an = (2* an - 2+ an-1) ifn is even
So for instance:
(n = 0) ao = 1
(n = 1) ai = (ao + 1) *2 = (1+ 1) *2= 4
(n = 2) az = (2* ao + ao) = 2*1+4 = 6
(n = 3) az = (az + 1) *2 = (6 + 1) *2 =14
The resulting sequence is 1, 4, 6, 14, .
if n is odd
!3!
%3D
You will write a function that returs the nth term of the sequence. You must do this by
using a recursive function. The function will NOT print out any values. Rather, the
function will return the nth term of the sequence using a recursive algorithm.
In the main part of the program:
1. Input the number of terms of the sequence to output.
2. In a for loop, call the function repeatedly to get the desired number of terms. The
function will take i (assuming the for index is called i) as the argument and return the ith
term of the sequence.
3. In the for loop, print out each term as it is returned.
Figure out the necessary prompts for the inputs and the desired outputs by looking at this
example session. The number in red is a possible input and is not what you print out.
Enter the number of terms> 4
Term #0> 1
Term #1> 4
Term #2> 6
Term #3> 14
HINTS:
1. The base case is when n==0
2. In the recursive case you will need to decide if n is odd or even.
n is odd if there is a remainder when you divide by two.
if (n % 2) !=0): #test for odd
Since a nonzero number is true, the above could be shortened to:
if (n % 2): #test for odd
Either way,
else: #must be even
expand button
Transcribed Image Text:Program #2 Define a sequence of numbers recursively Define a sequence ao, a1, az, a3, where ao = 1 an = (an -1+ 1) * 2 an = (2* an - 2+ an-1) ifn is even So for instance: (n = 0) ao = 1 (n = 1) ai = (ao + 1) *2 = (1+ 1) *2= 4 (n = 2) az = (2* ao + ao) = 2*1+4 = 6 (n = 3) az = (az + 1) *2 = (6 + 1) *2 =14 The resulting sequence is 1, 4, 6, 14, . if n is odd !3! %3D You will write a function that returs the nth term of the sequence. You must do this by using a recursive function. The function will NOT print out any values. Rather, the function will return the nth term of the sequence using a recursive algorithm. In the main part of the program: 1. Input the number of terms of the sequence to output. 2. In a for loop, call the function repeatedly to get the desired number of terms. The function will take i (assuming the for index is called i) as the argument and return the ith term of the sequence. 3. In the for loop, print out each term as it is returned. Figure out the necessary prompts for the inputs and the desired outputs by looking at this example session. The number in red is a possible input and is not what you print out. Enter the number of terms> 4 Term #0> 1 Term #1> 4 Term #2> 6 Term #3> 14 HINTS: 1. The base case is when n==0 2. In the recursive case you will need to decide if n is odd or even. n is odd if there is a remainder when you divide by two. if (n % 2) !=0): #test for odd Since a nonzero number is true, the above could be shortened to: if (n % 2): #test for odd Either way, else: #must be even
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education