Problem 1 A. Consider the following recursive method. def foo1(n):
      if n == 2:           return 1      else:           return 3 * foo1(n – 1) What value is returned as a result of the call foo1(5)? Show the recursive calls generated. B. Consider the following recursive method. def foo2(n):      
if n > 0:           print(n, end=’ ‘)           foo2(n-1)  What is the output of the call foo2(5)   Problem 2 Make sure your code is readable and well-documented. Each function must also begin with a title block that describes the task of the function, input parameters and return value.   Write a recursive function that takes a string as an input and returns the reverse of the string.          2. Write a recursive function rec_string that produces the output shown below for the corresponding function calls. Write a main function to test the function.   Method call rec_string(‘abcde’), will produce the following output: * e de cde bcde abcde   Method call rec_string(‘abc’), will produce the following output: * c bc abc   3. Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an important problem that arises in many commercial applications. We can efficiently compute the gcd using the following property, which holds for positive integers p and q:           If p > q, the gcd of p and q is the same as the gcd of q and p % q.

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

Problem 1

A. Consider the following recursive method.

def foo1(n):


     if n == 2:

          return 1

     else:

          return 3 * foo1(n – 1)

What value is returned as a result of the call foo1(5)? Show the recursive calls generated.

B. Consider the following recursive method.

def foo2(n):

     
if n > 0:

          print(n, end=’ ‘)

          foo2(n-1)

 What is the output of the call foo2(5)

 

Problem 2

Make sure your code is readable and well-documented. Each function must also begin with a title block that describes the task of the function, input parameters and return value.

 

  1. Write a recursive function that takes a string as an input and returns the reverse of the string.

 

       2. Write a recursive function rec_string that produces the output shown below for the corresponding function calls. Write a main function to test the function.

 

Method call rec_string(‘abcde’), will produce the following output:

*

e

de

cde

bcde

abcde

 

Method call rec_string(‘abc’), will produce the following output:

*

c

bc

abc

 

3. Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an important problem that arises in many commercial applications. We can efficiently compute the gcd using the following property, which holds for positive integers p and q:

          If p > q, the gcd of p and q is the same as the gcd of q and p % q.

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 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