Program #2 Define a sequence of numbers recursively Define a sequence as, at, az, as, where ao = 1 an = (an-1+ 1) 2 an = (2* an - 2 + aa -1) ifn is even So for instance: (n = 0) ao = 1 (n = 1) a; = (ao + 1) * 2 = (1+ 1)*2= 4 (n = 2) az = (2* ao + ao) = 2*1+4 = 6 (n = 3) as = (az + 1) *2= (6+ 1) * 2 =14 The resulting sequence is 1, 4, 6, 14, - %3D if n is odd %3D You will write a function that returis 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 terma> 4 Term 10> 1 Term 1> 4 Term 12> 6 Term 13> 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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
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
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Computational Systems
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education