Arithmetic Sequences & Series With Python
Hi everyone. This page is on working with arithmetic sequences and series with the Python programming language.
The original page is from my website here.
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, the n-th term in the arithmetic sequence can be found.
In math notation, the formula for the n-th term is:
The above math formula can be implemented into Python as a function. In the function, the inputs are the starting term, the number of terms and the common difference.
# 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, 7, 13, 19, 25, 31, 37, ...)
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:
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.")
In the example below, it is shown that the number of terms from the sequence 1, 2, 3, 4, 5, 6 up to 10 is 10. (Of course!)
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:
# 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)
print(find_arithmetic_seriesSum_verOne(start = 1, end = 10, n = find_numTerms_arithSeq(1, 10, 1)))
55.0
Math text rendered with QuickLaTeX.com. A good math textbook that I used in the past is Functions 11 by Nelson (Publisher in Ontario, Canada).