Arithmetic Sequence

Hide Notes ▼

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 $$

The Arithmetic Sequence Equation

So, can we write a program that prints any number in the sequence? The general equation that gives the value of the nth number of an arithmetic sequence is:
Arithmetic Sequence Equation $$ a_n = a_1 + d (n-1) \tag{2}$$
Where d is the common difference, in this case 2. So, for our example (sequence (1)), the equation is: $$ a_n = 4 + 2 (n-1) \tag{3}$$ Therefore the 7th term would be: $$ a_7 = 4 + 2 (7-1) $$ $$ a_7 = 4 + 2 (6) $$ $$ a_7 = 4 + 12 $$ $$ a_7 = 16 $$

Exercise 1

Write a program, using Equation (3), that finds the 7th value in the sequence.
Show Solution ▼

Exercise 2a

Now write a function that would take the initial value (an ), the common difference (d ), and a value of (n ), and returns the given number in the sequence.
Show Solution ▼

Exercise 2b

Print out the 15th number of the sequence: $$ a_n = 5, 11, 17, 23, ... $$
Show Answer ▼

Exercise 3a

Use your function and a loop to print out the first 10 terms in the sequence.
Show Solution ▼

Exercise 3b

Print out the first 10 numbers of the sequence: $$ a_n = 3, 7, 11, 15, ... $$
Show Answer ▼

The Sum of an Arithmetic Sequence

If we add the first four numbers of sequence (1), we would write it as:
Summation Notation $$ \sum^{4}_{n=1} a_n \tag{4} $$
Which for Sequence (1) would give: $$ \sum^{4}_{n=1} a_n = a_1 + a_2 + a_3 + a_4 $$ $$ \sum^{4}_{n=1} a_n = 4+6+8+10 = 28 $$ Similarly, the sum of the 5th through 10th number would be written as: $$ \sum^{10}_{n=5} a_n \tag{4} $$

Exercise 4

Write a program that sums the numbers in an arithmetic sequence given a starting number and a final number. Use it to calculate the sum in Equation 4. You may use the function you wrote for the previous exercises:
Show Solution ▼

Exercise 5

Use your program to get the sum of the third through 50th numbers in the sequence: $$ \sum^{50}_{n=3} a_n \tag{4} $$
Show Answer ▼

Exercise 6

Make a function that sums the numbers in a sequence (if you have not already) given a starting number and an ending number. Use it to find the sum of the 3rd through 50th numbers in Sequence (4):
Show Solution ▼

Classes

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.

Initialize

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.

Using Method: Get the 5th value.


      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
      

Using Method: get_sum

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