Related questions
Concept explainers
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
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)
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- Using pycharm/pythonarrow_forwardWrite the definition of a recursive function named starsCount that takes a string parameter and returns the number of star characters (i.e. *) in the string. Function MUST be recursive and does not use any global or static variables. Edit Format Table |BIU A v WP 12pt v Paragraph v O words ::::arrow_forwardphythonarrow_forward
- A. The following code in the function "is_prime" attempts to examine a number and return whether the number is prime (i.e. it has no factors besides 1 and itself). It has a "Boolean Flag" called 'prime', however, the boolean logic is not implemented correctly, so the function won't always return the correct answer. # This function determines whether an integer is prime.def is_prime(n):prime = Truefor i in range(2, n): if n2 % i == 0: prime = Falseelse: prime = truereturn prime In what cases does the function report an incorrect answer? How can the code be fixed for it to always report the correct answer? Write your answers as "comments" and the fixed code as a separate python filearrow_forwardWrite a function argument_count that accepts an integer i, followed by any number of additional arguments. The function should returnTrue if the value given to i is equal to the number of arguments that follows it, and should return False otherwise.arrow_forwardIn C++ write a program that generates three random numbers and then find the min number among the generated values, using these three functions: void getrandnum(int &n1, int &n2, int &n3), int findMin(int n1, int n2, int n3), and void printResult(int n1, int n2, int n3, int min). Make the main function drive all these functions.arrow_forward
- When a function accepts several arguments, how important is it what order they are sent in?arrow_forwardpython3 Write a recursive function called is_palindrome(string) that takes a string parameter and checks if it is a palindrome ignoring the spaces, if any, and returns True/False. Sample output:>>> print(is_palindrome("never odd or even"))True>>> print(is_palindrome("step on no pets"))Truearrow_forwardFor each function, describe what it actually does when called with a string argument. If it does not correctly check for lowercase letters, give an example argument that produces incorrect results, and describe why the result is incorrect. # 1 def any_lowercase1(s): for c in s: if c.islower(): return True else: return False # 2 def any_lowercase2(s): for c in s: if 'c'.islower(): return 'True' else: return 'False' # 3 def any_lowercase3(s): for c in s: flag = c.islower() return flag # 4 def any_lowercase4(s): flag = False for c in s: flag = flag or c.islower() return flag # 5 def any_lowercase5(s): for c in s: if not c.islower(): return False return True The code and its output must be explained technically whenever asked. The explanation can be provided before or after the code, or in the form of code comments within...arrow_forward
- 6. Given the following main function: // remove the first digit of a number int main() { int n, m; cout>n; m-removeFirst(n); cout <arrow_forwardWrite a function named contains-double in Scheme that takes a list as input and returns true if an item every appears twice in a row. You may assume the list is flat (i.e., no nested lists).arrow_forwardFunctions like print which perform an action but don’t return a value are called: Select one: a. recursive functions b. built-in functions c. simple functions d. utility functions e. void functionsarrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education