You might have heard about arithmetic sequences and geometric sequences in your mathematics classes. In this article, we will discuss arithmetic sequences. We will also implement programs to perform different operations on an arithmetic sequence in Python.
What is An Arithmetic Sequence?
An arithmetic sequence is a sequence of numbers in which any two consecutive numbers have a fixed difference. This difference is also known as the common difference between the terms in the arithmetic sequence.
For example, 3,5,7,9,11,13,… is an arithmetic sequence with a common difference of 2 between consecutive terms.
Nth Term In An Arithmetic Sequence
If we are given the first term A1 and the common difference D, we can write the second term as A1+D
, the third term as A1+2D
, the fourth term as A1+3D
, and so on. The Nth term will be written as A1+(N-1)D
To find the Nth term of an arithmetic sequence in python, we can simply add the common difference (N-1) times to the first terms A1 using a for loop as follows.
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating 100th term
N = 100
nthTerm = firstTerm
for i in range(1, N):
nthTerm = nthTerm + commonDifference
print("100th term in the arithmetic sequence is:", nthTerm)
Output:
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
100th term in the arithmetic sequence is: 201
Alternatively, we can directly calculate the Nth term using the formulae as follows.
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating 100th term
N = 100
nthTerm = firstTerm + (N - 1) * commonDifference
print("100th term in the arithmetic sequence is:", nthTerm)
Output:
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
100th term in the arithmetic sequence is: 201
Sum Of N Terms In An Arithmetic Sequence In Python
To find the sum of N terms in an arithmetic expression, we can simply add each term using a for loop. In the for loop, we will first find each term using the formulae discussed above. After that, we will add the each term to calculate the sum of N terms as follows.
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating sum of 50 terms
N = 50
sumOfTerms = 0
for i in range(1, N + 1):
ithTerm = firstTerm + (i - 1) * commonDifference
sumOfTerms = sumOfTerms + ithTerm
print("Sum of 50 terms in the arithmetic sequence is:", sumOfTerms)
Output:
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
Sum of 50 terms in the arithmetic sequence is: 2600
Alternatively, we can also derive a mathematical expression for calculating the sum of N terms of the arithmetic sequence.
We know that the sum of N numbers will be equal to N * (average of all the terms). Here, we can find the average of all the terms very easily.
For an arithmetic sequence with the first term A1 and the Nth term AN, the average of all the terms is defined as (A1+AN)/2
. As A1 and common difference D will be given in the program, we can find AN= A1+ (N-1)*D
.
Hence, the average of all the numbers in the arithmetic sequence will become (2A1+ (N-1)*D)/2
.
Subsequently, the sum of N terms of the arithmetic sequence will become N*((2A1+ (N-1)*D)/2)
.
We can calculate the sum of N terms in the arithmetic equation using this formula in python as follows.
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating sum of 50 terms
N = 50
sumOfTerms = (N * (2 * firstTerm + (N - 1) * commonDifference)) // 2
print("Sum of 50 terms in the arithmetic sequence is:", sumOfTerms)
Output:
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
Sum of 50 terms in the arithmetic sequence is: 2600
Conclusion
In this article, we have discussed the basics and formulas of arithmetic sequences. We have also performed different operations like finding the Nth term and finding the sum of N terms of an arithmetic sequence in python. To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.