<jupyter_start><jupyter_text>This Module is from the Maths and Programming Foundation Courses at Wolverhampton  SequencesA sequence is an ordered list of numbers. Every item can be labeled by its position, i.e.the 1st term, the 2nd term etc.By a sequence of real numbers we mean a function $f : N→R.$In a way, the sequence is the function but we usually return to the list idea and writethe sequence as $f(1), f(2), f(3)$, . . We can specify sequences in many ways. No matter what method we use we mustcommunicate what every term in the sequence is.• **For example using a general formula: ${2n}$.We multiply each term in the sequence by 2.**<jupyter_code>def seq_2n(arr):
    """This method returns a sequence
        where every item is multiplied by 2
        
    @param arr: A list, a sequence
    @return: A list
    """
    arr = [i*2 for i in arr]
    return arr

seq = [1, 5, 3, 9, 4]
seq_2n(seq)<jupyter_output><empty_output><jupyter_text>Using a recurrence: $x_0 = 0, x_n = 2 + x_{n−1}$. i.e.* $x_1 = 2 + x_{1−1}$* $x_1 = 2 + x_0$* $x_1 = 2 + 0$* $x_1 = 2$<jupyter_code>def recur_seq(n):
    """This method returns a sequence
        where every next item recurs from
        the previous item, based on the 
        recurrence rule. 
        
        This rule states that if a list has only
        one item, the term value must be 0, then for'
        every next item add 2 to the last item
        
    @param n: int, the length of an imaginary list
    @return: int, the value of the nth item in the seq
    """
    if n == 1:
        return 0
    
    return 2 + recur_seq(n-1)
lent = 6
seq = ['']*lent

for i in range(1, len(seq)+1, 1):
    seq[i-1] = recur_seq(i)
    
seq<jupyter_output><empty_output><jupyter_text>Arithmetic ProgressionsAn arithmetic progression is a sequence in which each term is obtainedfrom the preceding by adding a fixed amount. Every arithmetic progression is given bythe formula:$x_n = c + (n − 1)d$,and the terms of an arithmetic progression are$c$, $c + d$, $c + 2d$, $c + 3d$, . . .Where $c$ is the 1st term in the sequence, also called $x_1$, and $c + d$ is the 2nd term or $x_2$ and $c + 2d$ is the 3rd term or $x_3$ and so on...While $n$ is the index or position of the term in the list. for example the 1st term $x_1$ has $n=1-1$ in python but $n=1$ in other 1 based indexing programming languages like java and so on.<jupyter_code>def arith_prog(c, n, d):
    """Arithmetic Progression for 
      any seq or list
      
    @param c: int, value of 1st term in seq
    @param d: int, constant term to be added
    @param n: int, index of the term we seek
    @return: int, value of the term we seek
    """
    xn = c + (n - 1)*d
    
    return xn<jupyter_output><empty_output><jupyter_text>**Write a sequence that begins at 2 and adds 3 to each term in a sequence of length 8.**<jupyter_code>c = 2
d = 3
indexs = list(range(1, 9))
seq = [arith_prog(c, n, d) for n in indexs]
seq<jupyter_output><empty_output><jupyter_text>**Find an equation for the general term of the given arithmetic sequence anduse it to calculate it’s 70th term. 9, 6, 3, 0, −3, . . .** The formula is $x_n = c + (n - 1)d$* c = 9 ... 1st term* d = $x_2 - x_1 => 6 - 9 = -3$formula is... * $x_n = 9 + (n - 1)-3$* $x_n = 9 -3n + 3$* $x_n = 12 - 3n$<jupyter_code># We can use the above equation to find the 70th term or x_70
n = 70
x_70 = 12 - 3*n
x_70<jupyter_output><empty_output><jupyter_text>**Sometimes $x_1$ may not be given. Find the general term of an arithmeticsequence where $x_8$ = 44 and $x_{14}$ = 86** Here we need to find the first term $x_1$ as well as the constant term $d$ using system of equations* $x_n = c + (n - 1)d$* $x_8 = c + (8 - 1)d$, which is same as... $44 = c + (8 - 1)d$, same as $c + 7d = 44$* $x_{14} = c + (14 - 1)d$, which is same as... $86 = c + (14 - 1)d$, same as $c + 13d = 86$so we solve the 2 system of equations. let's solve for $d$ by eliminating $c$* $c + 13d = 86$* minus...* $c + 7d = 44$* $=> 6d = 42$... Therefore $d = 7$Now substitute $d = 7$ into any equ to get $c$* $c + 7d = 44$; => $c + 7*7 = 44$; => $c + 49 = 44$; => $c = 44 - 49$, finally... $c = -5$To get the general equation for this problem, we do...* $x_n = c + (n - 1)d$* $x_n = -5 + (n - 1)7$* $x_n = -5 + 7n -7$* $x_n = -12 + 7n$<jupyter_code># Now that we have both c and d, we can use them to find the values of seq

c = -5
d = 7

indexs = list(range(1, 15))
seq = [arith_prog(c, n, d) for n in indexs]
seq
# or we use the equation above to solve from x = 1 to 14

seq = [(-12 + 7*n) for n in indexs]
seq<jupyter_output><empty_output><jupyter_text>**Find all terms in between $x_1 = −5$ and $x_5 = 7$** Here, $c$ is given as -5, so we must find the constant term $d$...* $x_n = c + (n - 1)d$* $x_n = -5 + (5 - 1)d$, => * $7 = -5 + (5 - 1)d$, => * $7 = -5 + 4d$, => * $7+5 = 4d$, => $12/4 = d$, * $d = 3$With both $c$ and $d$ found, we can get the general equation as...* $x_n = -5 + (n - 1)3$* $x_n = -5 + (3n - 3)$* $x_n = -5 + 3n - 3$* $x_n = -8 + 3n$<jupyter_code># with this equation, we can find from x_1 to x_5

indexs = list(range(1,6))
seq = [(-8 + 3*n) for n in indexs]
seq<jupyter_output><empty_output><jupyter_text>Geometric ProgressionsA geometric progression is a sequence where each term is obtained fromthe previous term by multiplying it by a fixed amount.Every geometric progression isgiven by the formula$x_n = cr^{n−1}$.In general, we write the terms of a geometric sequence as$c$, $cr$, $cr^2$, $cr^3$, ... The constant factor $r$ is called the common ratio. Where $r = \frac{x_n}{x_{n−1}}$(one term in thesequence divided by the previous term)<jupyter_code>def geom_prog(c, r, n):
    """Geometric Progression for 
      any seq or list
      
    @param c: int, the value of 1st term
    @param r: int, the common ratio for all terms
    @param n: int, index of term we seek
    @return: int, value of term we seek
    """
    xn = c * r**(n-1)
    
    return xn<jupyter_output><empty_output><jupyter_text>**Write a sequence that begins at 2 and is multiplied by 3 each term for 5 terms.**<jupyter_code>c = 2
r = 3

# return the sequence of geometric progression
seq = [geom_prog(c, r, n) for n in indexs]
seq<jupyter_output><empty_output><jupyter_text>**Find all terms between $x_1 = 5$ and $x_5 = 103680$ of a geometric sequence.** here, we need to find $r$ using system of equations* $c = 5$* $x_n = cr^{n-1}$* $103680 = 5r^{5-1}$* $103680 = 5 * r^4$* $\frac{103680}{5} = r^4$* $20736 = r^4$* $r^4 = 20736$* $r^4 = 12^4$* $r = 12$With $c$ and $r$ found, let's define a general equation for solving any values of $n$* $x_n = cr^{n-1}$* $x_n = 5* 12^{n-1}$<jupyter_code>indexs = list(range(1,6))
c = 5
r = 12

seq = [(5 * 12**(n-1)) for n in indexs]
seq
# or using the geom formula

seq = [geom_prog(c, r, n) for n in indexs]
seq<jupyter_output><empty_output><jupyter_text>**Write a sequence that begins at 2 and is multiplied by 3 each term, for 5 terms.**<jupyter_code>c = 2
r = 3

# Using Geometric Progression
seq = [geom_prog(c, r, n) for n in indexs]
seq<jupyter_output><empty_output><jupyter_text>**Find the general term for the given geometric sequence and use it to calculateit’s 6th term.$3$, $\frac{6}{5}$, $\frac{12}{25}$, . . .** We need to first find $r$ using the terms given...* $c = 3$* $3 * r = \frac{6}{5}$* $3r = \frac{6}{5}$* $3r = 1.2$* $r = \frac{1.2}{3}$* $r = 0.4$Then we can find the general term as:* $x_n = cr^{n-1}$* $x_n = 3 * 0.4^{n - 1}$<jupyter_code># we can find the 6th term as...

n = 6
x6 = 3 * 0.4**(n-1)
x6
# Let's check if the given x1 is correct...

n = 1
x1 = 3 * 0.4**(n-1)
x1<jupyter_output><empty_output><jupyter_text>SeriesIn mathematics, the distinction between **sequences** and **series** is that a series is the sumof the terms of a sequence Sequences of Partial Sums $s_n = {\sum_{i=1}}^n{x_i}$Given a sequence{s} = $[x_1, x_2, x_3, x_4]$To find $s_4$ we sum the terms $x_i$ from $i = 1$ to $n = 4$... $(x_1 + x_2 + x_3 + x_4)$ Consider {2n}= $[2, 4, 6, 8, 10, 12, 14, . . .]$ $s_n = {\sum_{i=1}}^n{2i}$  Arithmetic SeriesAn arithmetic series is the sum of the terms of an arithmetic progression.The partial sum of an arithmetic sequence is the sum of a finitenumber of terms within an arithmetic sequence. This is given by, in general... $s_n = {\sum_{i=1}}^n{x_i} => \frac{n(x_1 + x_n)}{2}$<jupyter_code># Given a sequence
seq = [2,4,6,8,10,12,14]

# find the sum of the first 5 terms
x1 = 2
n = 5
xn = 10
def arith_series(x1, xn, n):
    """This method sums an arithmetic sequence
    """
    ser = (n * (x1 + xn)) // 2
    
    return ser

arith_series(x1, xn, n)<jupyter_output><empty_output><jupyter_text>**Sum the first 100 terms of the sequence defined by $x_n = 2 + (n − 1)3$**<jupyter_code>n = 100
c = 2  # first term or x1
d = 3

# since we have the 1st term, we must find the 100th term
x100 = arith_prog(c, n, d)
x100
# now we apply the arithmetic seq summation method to get first 100 terms summ
arith_series(c, x100, 100)<jupyter_output><empty_output><jupyter_text>**Find the 35th partial sum, $S_{35}$ of the arithmetic sequence $a_n = \frac{1}{2}n + 1$**<jupyter_code>x1 = 0.5*1 + 1
n = 35
x35 = 0.5*n + 1
print(f"x1: {x1}, x35: {x35}")
# To find the 35th partial sum...
_35thsum = arith_series(x1, x35, n)
_35thsum<jupyter_output><empty_output><jupyter_text>**Find the sum of the first 50 terms of the arithmetic sequence 4, 9, 14, 19, 24, . . .**<jupyter_code>n = 50
c = 4  # which is 1st term or x1
d = 5  # 9 - 4 or 14 - 9...

# we need to find x50...
x50 = c + (n - 1)*d
x50
# Now that we know the 50th term value, we can sum all em up...

sum_50_terms = arith_series(c, x50, n)
sum_50_terms<jupyter_output><empty_output><jupyter_text>Geometric SeriesA geometric series is the sum of the terms of a geometric sequence.To find the partial sum of a geometric sequence in general is: $s_n = \frac{c(1-r^n)}{1-r}$where $c$ is the first item in the sequence, $n$ is the term whose item or value we seek, and $r$ is the common ratio. this fornula holds that $r \ne 1$<jupyter_code>def geom_series(c, r, n):
    """Sum the partial sequence
        of a geometric progression
        
    @param c: int, the value of 1st term
    @param r: int, the common ratio for all terms
    @param n: int, index of term we seek
    @return: int, value of term we seek
    """
    assert r != 1, 'Error r cannot == 1'
    
    sn = (c * (1 - r**n)) // (1 - r)
    return sn<jupyter_output><empty_output><jupyter_text>Sum the first 5 terms of the sequence defined by **$x_n = 2(3)^{n−1}$**<jupyter_code># first term
c = 2
r = 3

# for the 5th term
n = 5 
x_n = geom_prog(c, r, n)
x_n
# Now that we know the 1st and 5th terms, we can sum them
# Using the geometric series function

geom_sum_5th = geom_series(c, r, n)
geom_sum_5th<jupyter_output><empty_output><jupyter_text>let's see all first five terms gotten from the geometric progression formula above<jupyter_code>[geom_prog(c, r, n) for n in indexs]<jupyter_output><empty_output><jupyter_text>Sum the first 20 terms of the sequence defined by $a_n = 3(−2)^n$<jupyter_code># to find the 1st item or c...

r = -2  # seen from the formula above
n = 1
c = 3 * (r)**n

# to find the sum of the first 20 terms using geom-series function
n = 20
geom_sum_20 = geom_series(c, r, n)
geom_sum_20<jupyter_output><empty_output><jupyter_text>Find the 15th partial sum, $S_{15}$ of the geometric sequence $x_n = 2(3)^{n−1}$<jupyter_code># From the complete geom formula above, we see that...

c = 2
r = 3

# Therefore to find the 15th partial sum using geom-series
n = 15
geom_sum15th = geom_series(c, r, n)
geom_sum15th<jupyter_output><empty_output><jupyter_text>Find $a_n$ if $s_4 = \frac{26}{27}$ and $r = \frac{1}{3}$note that: $s_n = \frac{c(1-r^n)}{1-r}$ First we find $s_1$ or it's value called $c$... * $\frac{26}{27} = \frac{c(1 - {\frac{1}{3}^4)}}{1 - \frac{1}{3}}$ * $0.9629 = \frac{c(0.9876)}{0.6666}$ * $0.9629 * 0.6666 = c(0.9876)$ * $0.6419 = c(0.9876)$ * $c(0.9876) = 0.6419$ * $c = \frac{0.6419}{0.9876}$ * $c = 0.65$ Now that we know both $c$ and $r$ we can define the formula for $a_n$ as... * $a_n = c(r)^{n-1}$ * $a_n = 0.65 * (\frac{1}{3})^{n - 1}$  * $a_n = 0.65 * (0.3333)^{n - 1}$   Quiz 1. Recall, every arithmetic progression is given by$a_n=a_1+(n-1)d$Find an equation for the general term of the given arithmetic sequence and use it to calculate its 100th term$4,10,16,22,28,\dots$<jupyter_code>d = 6
a1 = 4
n = 100
arith_prog(a1, n, d)<jupyter_output><empty_output><jupyter_text>2. For the given arithmetic progression$-5, a_2, a_3,a_4, 7$find: $a_2, a_3, a_4$<jupyter_code># therefore we find d using elimination...
d = 3
c = -5
indexs = list(range(1, 6))
[arith_prog(c, n, d) for n in indexs]<jupyter_output><empty_output><jupyter_text>3. Recall, every geometric progression is given by the formula$a_n = a_1r^{n-1}$where $r = \cfrac{a_n}{a_{n-1}}$Find an equation for the general term of the given sequence and use it to calculate its 11th term.$2,6,18,54,\dots$<jupyter_code>c = 2
r = 3
n = 11
geom_prog(c, r, n)<jupyter_output><empty_output><jupyter_text>4. Recall, the partial sum of an arithmetic sequence is given by$S_n=\cfrac{n(a_1+a_n)}{2}$ Find the sum of the first 60 terms of the arithmetic sequence$8,19,30,41,52,\dots$<jupyter_code>c = 8
d = 11
n = 60
x60 = arith_prog(c, n, d)
x60
# Therefore sum of first 60 terms of arithmetic progression is...
arith_series(c, x60, n)<jupyter_output><empty_output><jupyter_text>5. Recall, to find the partial sum of a geometric sequence$S_n = \cfrac{a_1(1-r^n)}{1-r}$where $r \neq 1$ Find the sum of the first 10 terms of the given geometric sequence$4, -8, 16, -32, 64, \dots$<jupyter_code>c = 4
r = -2
n = 10

# sum of first 10 terms of geom prog is...
geom_series(c, r, n)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1454452852153667, 'fraction_numerical': 0.05515133876600699, 'mean_word_length': 3.601606963508537, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '89783', 'license': 'MIT', 'n_tokens_mistral': 5834, 'n_tokens_neox': 5277, 'n_words': 1982, 'path': '2.sequences_and_series.ipynb', 'repo_name': 'Lawrence-Krukrubo/Foundational_Maths_for_AI', 'size': '13744'}