# Tutorial

We will solve the following problem using a computer to using a programming
technique called **recursion**.

```{admonition} Problem

A sequence $a_1, a_2, a_3, …$ is defined by:

$$
    \left\{
    \begin{array}{l}
        a_1 = k,\\
        a_{n + 1} = 2a_n – 7, n \geq 1,
    \end{array}
    \right.
$$

where $k$ is a constant.


1. Write down an expression for $a_2$ in terms of $k$.
2. Show that $a_3 = 4k -21$
3. Given that $\sum_{r=1}^4 a_r = 43$ find the value of $k$.
```

We will use a Python to define a function that reproduces the mathematical
definition of $a_k$:


```python
def generate_a(k_value, n):
    """
    Uses recursion to return a_n for a given value of k:

    a_1 = k
    a_n = 2a_n - 7
    """
    if n == 1:
        return k_value
    return 2 * generate_a(k_value, n - 1) - 7
```

```{attention}
This is similar to the mathematical definition the Python definition of
the function refers to itself.
```


We can use this to compute $a_3$ for $k=4$:


```python
generate_a(k_value=4, n=3)
```




    -5



We can use this to compute $a_5$ for $k=1$:


```python
generate_a(k_value=1, n=5)
```




    -89



Finally it is also possible to pass a symbolic value to `k_value`. This allows
us to answer the first question:


```python
import sympy as sym

k = sym.Symbol("k")
generate_a(k_value=k, n=2)
```




$\displaystyle 2 k - 7$



Likewise for $a_3$:


```python
generate_a(k_value=k, n=3)
```




$\displaystyle 4 k - 21$



For the last question we start by computing the sum:

$$
    \sum_{r=1}^4 a_r = 43
$$


```python
sum_of_first_four_terms = sum(generate_a(k_value=k, n=r) for r in range(1, 5))
sum_of_first_four_terms
```




$\displaystyle 15 k - 77$



This allows us to create the given equation and solve it:


```python
equation = sym.Eq(sum_of_first_four_terms, 43)
sym.solveset(equation, k)
```




$\displaystyle \left\{8\right\}$



```{important}
In this tutorial we have

- Defined a function using recursion.
- Called this function using both numeric and symbolic values.
```

266a214709445fd83de9ff82fe111269adf7c82c
ipynb
Jupyter Notebook
book/tools-for-mathematics/07-sequences/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
book/tools-for-mathematics/07-sequences/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
book/tools-for-mathematics/07-sequences/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
Qwen/Qwen-72B
1. YES
2. YES