1 Problem An integer sequence is a sometimes infinite chain of integer values. I
ID: 3751791 • Letter: 1
Question
1 Problem An integer sequence is a sometimes infinite chain of integer values. In some cases the later values in the sequence are computed from earlier values in the sequence. An example of such a sequence is the Fibonacci sequence. Starting from 0 and 1, the sequence's next value is the sum of the previous two numbers. Thus we have 0,1,1,2,3,5,8, 13,21, and on to infinity This assignment concerns itself with the "double plus 5" sequence. This sequence starts with any non-negative integer, 0, 1, and so on. If the start value is vVo, the number after the start value has a value of vo x 2+5, and the number after that continues the pattern 1.1 Functions to Write print_sequence rec( start, count ) print_sequence iter( start, count ) . find end_rec( start, count ) find end_iter( start, count ) . find_start_iter( goal, count ) find_start_rec( goal, count)Explanation / Answer
def print_sequence_rec(start,count):
"""This is a recursive function
to find the given sequence"""
if count==0:
return 1
elif start<0:
return 1
else:
print(start)
return (print_sequence_rec(2*start+5,count-1))
print_sequence_rec(1,4)
---------------------------------------------------------------------------
def find_end_rec(start,count):
"""This is a recursive function
to find the end in the given sequence"""
if count==1:
print ("The end of sequence is:", start)
return 1
elif start<0:
return 1
else:
return (find_end_rec(2*start+5,count-1))
find_end_rec(1,2)
----------------------------------------------------
def find_start_rec(goal,count):
"""This is a recursive function
to find the start in the given sequence"""
if count==1:
print ("The start of sequence is:", goal)
return 1
else:
return (find_start_rec((goal-5)/2,count-1))
find_start_rec(43,4)
-----------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.