Hi there. This page is on working with arithmetic sequences and series in the Python programming language.

__Arithmetic Sequences In Python__

In mathematics, arithmetic sequences involve a list of ordered numbers with a common difference. Examples include:

- 1, 3, 5, 7, 9, 11, 13, 15, 17, …
- 1, 11, 21, 31, 41, 51, …
- -2, -4, -6, -8, -10, …

__Finding The n-th Term In An Arithmetic Sequence__

Given the first number in an arithmetic sequence and a common difference, you can find the n-th term in the arithmetic sequence.

In math notation, the formula for the n-th term is:

where:

- is the n-th term in the arithmetic sequence
- is the first term in the arithmetic sequence
- is the number of terms in the sequence from to
- is the common difference

The above math formula can be implemented into Python as a Python function. In the function, the inputs are the starting term, the number of terms and the common difference.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Formula: # Arithmetic Sequence Formula: end = start + (n - 1)d # end - start = (n - 1)d # (end - start) / d = n - 1 # (end - start) / d + 1 = n def find_term_n(start, n, diff): if start % 1 != 0: print("Please choose an integer.") elif n % 1 != 0 and n < 1: print("Please choose a positive whole number.") else: term_n = start + (n - 1)*diff return term_n |

In the example below, I find the 10th term in the arithmetic sequence with the first term as one and the common difference as 6.

1 2 3 |
print(find_term_n(start = 1, n = 10, diff = 6)) 55 |

__Finding The Number Of Terms In An Arithmetic Sequence__

Another function in Python dealing with the number of terms in an arithmetic sequence can be made. Given the starting term, the end term and the common difference in the arithmetic sequence, the number of terms can be found.

For finding the number of terms in an arithmetic sequence the math formula is:

1 2 3 4 5 6 7 8 |
def find_numTerms_arithSeq(start, end, diff): if start % 1 != 0: print("Please choose an integer.") numTerms = 1 + (end - start) / diff if numTerms > 0: return int(numTerms) else: print("The number of terms cannot be negative. Try again.") |

As shown in the example below, the number of terms from the sequence 1, 2, 3, 4, 5, 6 up to 10 is 10. (Duh.)

1 2 3 |
print(find_numTerms_arithSeq(1, 10, 1)) 10 |

__Arithmetic Series In Python__

An arithmetic sequence is a list of (ordered) numbers with a common difference. The sum of the numbers in an arithmetic sequence is an arithmetic series.

An example of an arithmetic sequence is 3, 6, 9, 12, 15, 18, 21 up to 39. The arithmetic series of those numbers would have plus signs instead of commas. That is, 3 + 6 + 9 + 12 + 15 + 18 + 21 + 24 + 27 + 30 + 33 + 36 + 39.

The formula for the arithmetic series is:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Arithmetic Series formula: # Finding the sum of an arithmetic series. # Reference: https://stackoverflow.com/questions/21583758/how-to-check-if-a-float-value-is-a-whole-number # def find_arithmetic_seriesSum_verOne(start, end, n): if start % 1 == 0 and start > 0: if end % 1 == 0 and end > 0 and end > start: total = 0.5*n*(start + end) return total else: print("Choose a starting number that is a whole positive number please.") |

The sum of the whole number integers from 1 to 10 is 55 as shown in the output below. Note that the argument for n uses a function call on the find_numTerms_arithSeq(1, 10, 1) that was done earlier. (1 + 2 + 3 + … + 10 = 55)

1 2 3 |
print(find_arithmetic_seriesSum_verOne(start = 1, end = 10, n = find_numTerms_arithSeq(1, 10, 1))) 55.0 |