”In mathematics, summation (capital Greek sigma symbol: Σ) is the addition of a sequence of numbers; the result is their sum or total.”
Source: https://en.wikipedia.org/wiki/Summation
Questions (20 min)
start
time:
Consider how to calculate
Write out all the numbers that need to be added:
Show how this sum can be calculated in terms of a smaller summation.
Write an expression similar to #11b showing how any summation of n integers can be calculated in terms of a smaller summation.
What is the base case of the summation? (Write the complete formula, not just the value.)
Here are important questions to consider before writing a recursive function:
How can you define the problem in terms of a smaller similar problem?
What is the base case, where you solve an easy problem in one step?
For the recursive call, how will you make the problem size smaller?
To avoid infinite recursion, make sure that each recursive call brings you closer to the base case!
Implement a recursive function named summationthat takes a parameter n and returns the sum 1 + 2 + … + n. It should only have an if statement and two returnstatements (no loops).
Enter your code into a Python Editor, and test the function. Make sure that summation(100)correctly returns 5050.
Implement a recursive function named geometricthat takes three parameters (a, r, and n) and returns the sum “a + ar + ar^2 + ar^3 + …” where n + 1 is the total number of terms.
What is the base case?
What is the recursive case?
Write the function in Python:
Enter your code into a Python Editor, and test the function. For example, if a=10 and r=3, the first five terms would be 10, 30, 90, 270, and 810. Make sure that geometric(10, 3, 4)correctly returns 1210.
The Correct Answer and Explanation is :
To address the questions and tasks:
1. Summation of Integers
The sum of the first n integers can be represented as:
[
S(n) = 1 + 2 + 3 + \dots + n
]
We want to express this sum in terms of a smaller summation. Notice that the sum of the first n integers can be broken into two parts:
[
S(n) = n + S(n-1)
]
Here, the smaller summation involves the sum of the integers from 1 to n-1, which is S(n-1). We can keep applying this recursively until we reach the base case.
The base case for this recursive summation is when ( n = 1 ):
[
S(1) = 1
]
The recursive case works by reducing the problem size. To avoid infinite recursion, each recursive call decreases the problem size by 1, bringing us closer to the base case.
2. Recursive Python Function for Summation
Here’s a Python implementation of the recursive function for summation:
def summation(n):
if n == 1:
return 1
else:
return n + summation(n - 1)
# Test the function with n = 100
print(summation(100)) # Expected output: 5050
3. Geometric Series Sum
The sum of a geometric series is represented as:
[
S_{\text{geo}}(a, r, n) = a + ar + ar^2 + ar^3 + \dots
]
Where a is the first term, r is the common ratio, and n is the number of terms. The recursive formula for this series is:
[
S_{\text{geo}}(a, r, n) = a + r \times S_{\text{geo}}(a, r, n – 1)
]
The base case is when ( n = 0 ), where no further terms exist:
[
S_{\text{geo}}(a, r, 0) = a
]
The recursive case reduces the number of terms in each step by 1.
4. Recursive Python Function for Geometric Series
Here’s a Python implementation for the geometric series sum:
def geometric(a, r, n):
if n == 0:
return a
else:
return a + r * geometric(a, r, n - 1)
# Test the function with a = 10, r = 3, n = 4
print(geometric(10, 3, 4)) # Expected output: 1210
Explanation:
- Summation function: This function adds up the integers from 1 to
n. Each recursive call reducesnby 1, summing the currentnwith the result of summing the integers up ton-1. - Geometric series function: This function computes the sum of the geometric series
a + ar + ar^2 + ...recursively. It multiplies the previous term byr(the common ratio) and keeps reducingnby 1 until we reach the base case wheren = 0.
Both of these functions rely on recursive calls and base cases to avoid infinite recursion and calculate the required sums.
Now, I will generate an image to illustrate this recursive process.
Here is an illustration of the recursive function process, showing the breakdown of summation and geometric series. It helps visualize how recursion works step by step with the summation and geometric series examples.
Let me know if you need further explanations or have more questions!
