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

Problem Statement for Recursive Sum of Numbers Program

Here is a simple recursive problem: Design a function that accepts a positive integer >= 1 and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 10 is passed as an argument, the function will return 55. Use recursion to calculate the sum. Write a second function which asks the user for the integer and displays the result of calling the function.

Part 1. Understand the Problem

To design a recursive function, you need to determine at least one base case (the base case returns a solution) and a general case (the general case calls the function again but passes a smaller version of the data as a parameter). To make this problem recursive think about it like this:

If the integer is 1, the function will return 1.

If the integer is 2, the function will return 1 + 2 = 3.

If the integer is 3, the function will return 1 + 2 + 3 = 6.

. . .

For this problem, we will be sending in the "last" integer as the parameter.The function will have to call itself recursively, passing a parameter one less than the number itself, and continue doing this until the parameter is 1. That is when the recursion can stop because the function will return a 1 without making a recursive call. The sum will"build up" from the bottom. The algorithm would look like this:

If n = 1, sum(n) = 1

If n > 1, sum(n) = n + sum(n-1)

To see how this works, assume n = 6. After the first call to sum(6), the function returns 6 + sum(5). However sum(5) is another function call, so this call must be made and completed before 6 + sum(5) can be calculated. When sum(5) is called, it will return 5 + sum(4) which cannot be evaluated until after sum(4) is called and returns. The call to sum(4) returns 4 + sum(3) which cannot be evaluated until after sum(3) is called and returns. When sum(3) is called it returns 3 + sum(2) and when sum(2) is called it returns 2 + sum(1). Finally, when sum(1)is called it can return a 1 without making another recursive call. The 1 gets returned to sum(2) and now sum(2) can be evaluated and returns 2 + 1 = 3 to sum(3). The function sum(3) can now calculate 3 + 3 and return 6 to sum(4). The function sum(4) can now calculate 4 + 6 and return 10 to sum(5). The function sum(5) can now calculate 5 + 10 andreturn 15 to sum(6) which can now calculate 6 + 15 = 21 which is the sum of 1 + 2 + 3 + 4 + 5 + 6.

(PYTHON)

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
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