Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Module 2 : Lab 3 - Review Linear Recursion Linear Recursion This lab is made to help you review / learn basic recursion. The next
Module : Lab Review Linear Recursion
Linear Recursion
This lab is made to help you reviewlearn basic recursion. The next lab will get a little trickier as you move from linear to branching
recursion.
What is recursion?
Linear recursion is when a function calls itself without branching out into multiple function calls. A good example of this would be a
factorial function,
Why is factorial linear in a recursive sense? Lets take for example. To compute this we need to do:
In other words we are doing or and so on until we get to which is considered to be the base case.
This function will only have one recursive call to itself, meaning if you draw out the stack trace each call will point to another in a linear
fashion.
This displays two important definitions: the recursive call and the base case.
A recursive call is when a function in code calls itself, and the base case is a condition in which causes the function to eventually
terminate. Without a base case, a recursive function will go indefinitely until you get what is called a stack overflow too many recursive
calls for the stack to handle Java will throw a runtime error if you create a recursive function which overflows.
Lab Assignment
The best way to get an idea of how recursion works is to practice. In the code given to you, complete the unfinished methods and run them
with the given test cases.
The following java files are included in this lab:
L
LinearRecursion.java
The Factorial Method
The first method we are going to modify is the Factorial Method.
TO DO:
Write this method so that it returns the factorial for any input integer is
The Summation Method
The next method we are going to modify is the Summation Method. This method is similar to factorial but instead of multiplying each
element you will add them together.The Summation Method
The next method we are going to modify is the Summation Method. This method is similar to factorial but instead of multiplying each
element you will add them together.
TO DO:
Write this method so that when you input an integer the method will return dots
The Harmonic Summation Method
The Harmonic Summation will return the sum of the harmonic series.
TO DO:
Set up this method so that when you input an integer the method will return the sum of dots
The Geometric Summation Method
The Geometric Summation will return the sum of the geometric series.
TO DO:
Set up this method so that when you input an integer the method will return the sum of the sum dots
Mathpow
To understand how these methods are exhibiting linear recursion, draw out the stack trace for a reasonable value like Here's an
example of this for factorial for :If you've implemented everything correctly your output should look like this:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started