Arithmetic Sequences & Series In Python

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:

    \[a_n = a_1 + (n - 1)d\]

where:

  • a_n is the n-th term in the arithmetic sequence
  • a_1 is the first term in the arithmetic sequence
  • n is the number of terms in the sequence from a_1 to a_n
  • d 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.

 

 

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.

 

 

 

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:

    \[n = \dfrac{a_n - a_1}{d} + 1\]

 

 

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.)

 

 

 


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:

    \[S_n = \dfrac{n}{2}(a_1 + a_n)\]

 

 

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)

 

 

Leave a Reply