An arithmetic sequence is a series of numbers with a constant, common difference between each one. For example: $$ a_n = 4, 6, 8, 10, 12, ... \tag{1}$$ Here the starting value is 4 and the difference between each successive pair of numbers is 2. The notation for the first number would be: $$ a_1 = 4 $$ and the 4th number: $$ a_4 = 10 $$
seq1.py
a_1 = 4
d = 2
n = 7
a_n = a_1 + d * (n-1)
print(a_n)
Resulting in:
16
seq2.py
def get_nth(a_1, d, n):
nth = a_1 + d * (n-1)
return nth
a_n = get_nth(4, 2, 7)
print(a_n)
My function's name is get_nth, and it takes three inputs: the initial value (a_1), the common difference (d), and the number of the sequence we're looking for. The result:
16
89
seq3.py
def get_nth(a_1, d, n):
nth = a_1 + d * (n-1)
return nth
for n in range(1, 11):
a_n = get_nth(4, 2, n)
print(n, "-->", a_n)
Resulting in:
1 --> 4
2 --> 6
3 --> 8
4 --> 10
5 --> 12
6 --> 14
7 --> 16
8 --> 18
9 --> 20
10 --> 22
1 --> 3
2 --> 7
3 --> 11
4 --> 15
5 --> 19
6 --> 23
7 --> 27
8 --> 31
9 --> 35
10 --> 39
seq4.py
def get_nth(a_1, d, n):
nth = a_1 + d * (n-1)
return nth
total = 0
for n in range(1, 5):
a_n = get_nth(4, 2, n)
total = total + a_n
print(n, a_n, total)
print("Sum = ", total)
Note that have the variable total that starts off as zero before the loop, and is added to during every iteration. The result:
1 4 4
2 6 10
3 8 18
4 10 28
Sum = 28
You'll notice
2640
seq6.py
def get_nth(a_1, d, n):
nth = a_1 + d * (n-1)
return nth
def get_sum(a_1, d, n1, n2):
total = 0
for n in range(n1, n2+1):
a_n = get_nth(a_1, d, n)
total = total + a_n
return total
sum = get_sum(4, 2, 3, 50)
print("Sum from 3 to 50 =", sum)
Resulting in:
Sum from 3 to 50 = 2640
We now have two functions that relate to arithmetic sequences, and there is some repetetion in that we have to tell them both the same information (a1 and d). This is where creating a class becomes useful.
A class is a way to gather related information and functions into a single entity that makes them easier to work with. Here, I'm going to create an arithmetic sequence class and use it to store and organize all the information and functions related to arithmetic sequences.
seqClass1.py
class arithmeticSeq:
#works with arithmetic sequences
def __init__(self, a_1, d):
#save values internally
self.a_1 = a_1 # inital value in sequence
self.d = d # common difference
def get_nth(self, n):
nth = self.a_1 + self.d * (n-1)
return nth
def get_sum(self, n1, n2):
total = 0
for n in range(n1, n2+1):
a_n = self.get_nth(n)
total = total + a_n
return total
# Program starts here
# Set up our specific sequence (instantiate the class)
seq = arithmeticSeq(4, 2)
# Get the 5th value in sequence (use a method from the class)
a_5 = seq.get_nth(5)
print("5th term:", a_5)
# Find the sum of the first four values in the sequence
sum = seq.get_sum(1, 4)
print("Sum of first 5 terms:", sum)
Let's parse this in detail, but we'll start from near the bottom when we first use the class.
To use the class we create an instance of it (on line 23) and give it the necessary information for the specific arithmetic sequence we want (a1 = 4 and d = 2):
class arithmeticSeq:
#works with arithmetic sequences
def __init__(self, a_1, d):
#save values internally
self.a_1 = a_1 # inital value in sequence
self.d = d # common difference
def get_nth(self, n):
nth = self.a_1 + self.d * (n-1)
return nth
def get_sum(self, n1, n2):
total = 0
for n in range(n1, n2+1):
a_n = self.get_nth(n)
total = total + a_n
return total
# Program starts here
# Set up our specific sequence (instantiate the class)
seq = arithmeticSeq(4, 2)
# Get the 5th value in sequence (use a method from the class)
a_5 = seq.get_nth(5)
print("5th term:", a_5)
# Find the sum of the first four values in the sequence
sum = seq.get_sum(1, 4)
print("Sum of first 5 terms:", sum)
We give this specific sequence (instance) a variable name (seq).
In creating our instance of the class the computer looks for the class name (arithmeticSeq) and then for the initialization function called __init__ (Line 4). This is where everything is set up for future use.
class arithmeticSeq:
#works with arithmetic sequences
def __init__(self, a_1, d):
#save values internally
self.a_1 = a_1 # inital value in sequence
self.d = d # common difference
def get_nth(self, n):
nth = self.a_1 + self.d * (n-1)
return nth
def get_sum(self, n1, n2):
total = 0
for n in range(n1, n2+1):
a_n = self.get_nth(n)
total = total + a_n
return total
# Program starts here
# Set up our specific sequence (instantiate the class)
seq = arithmeticSeq(4, 2)
# Get the 5th value in sequence (use a method from the class)
a_5 = seq.get_nth(5)
print("5th term:", a_5)
# Find the sum of the first four values in the sequence
sum = seq.get_sum(1, 4)
print("Sum of first 5 terms:", sum)
A function within a class is called a method.
You will notice that the __init__ method definition (Line 4) has 3 parameters, while the instantiation call (Line 23) only passes two varaibles. The first variable in the __init__ method is "self", which is the way that the methods within a class can pass all the internal information to each other.
The __init__ method saves the initial value and common difference for future use as internal variables. These internal variables start with self:
self.a_1 = a_1 # inital value in sequence
These internal variables like self.a_1 do not have to have the same names as the method inputs, but it's often convenient to have them so.
class arithmeticSeq:
#works with arithmetic sequences
def __init__(self, a_1, d):
#save values internally
self.a_1 = a_1 # inital value in sequence
self.d = d # common difference
def get_nth(self, n):
nth = self.a_1 + self.d * (n-1)
return nth
def get_sum(self, n1, n2):
total = 0
for n in range(n1, n2+1):
a_n = self.get_nth(n)
total = total + a_n
return total
# Program starts here
# Set up our specific sequence (instantiate the class)
seq = arithmeticSeq(4, 2)
# Get the 5th value in sequence (use a method from the class)
a_5 = seq.get_nth(5)
print("5th term:", a_5)
# Find the sum of the first four values in the sequence
sum = seq.get_sum(1, 4)
print("Sum of first 5 terms:", sum)
We have inserted the function get_nth into the class (Lines 9-11), so now it's called a method of the arithmeticSeq class. With our instance of the class created, and called seq, we can use the get_nth method to get the 5th term like so:
a_5 = seq.get_nth(5)
You don't need to pass the initial value and the common difference because the class already knows. However, you do have to refer to these internal variables using the self. prefix (e.g. Line 10):
def get_nth(self, n):
nth = self.a_1 + self.d * (n-1)
return nth
Similarly, we can use the get_sum method to find the sum of the first four numbers in the sequence:
sum = seq.get_sum(1, 4)
You should notice on Line 16 that when we call the get_nth method from within another method in the class we have to use the self prefix:
def get_sum(self, n1, n2):
total = 0
for n in range(n1, n2+1):
a_n = self.get_nth(n)
total = total + a_n
return total