Skip to content
Home » Blog » How to Find the nth Term of an Arithmetic Progression in Python

How to Find the nth Term of an Arithmetic Progression in Python

Introduction

Have you ever worked with a list of numbers that follow a pattern, like this one:
2, 5, 8, 11, 14...? This is called an Arithmetic Progression (AP). The cool thing about an AP is that the difference between each number is the same—in this case, it’s 3.

Now, let’s say you want to find the 100th term in this sequence. Instead of listing out all the numbers one by one (which would take forever!), you can use a simple formula to jump straight to the answer. And that’s exactly what we’re going to do in this post.

Why is this useful? Well, in coding, it’s often important to find a term in a sequence quickly and efficiently. If you had to calculate the 5000th term by adding up each number one by one, it would be really slow. But with the formula, you can calculate any term instantly, without wasting time or memory.

In this post, I’ll walk you through how to find the nth term of an arithmetic progression using Python. It’s simple, quick, and super handy when working with patterns or sequences in coding.

Diagram showing an arithmetic progression sequence with terms -2, 0, 2, 4, labeled with the first term (a) and successive terms (a+d, a+2d, a+3d).
Arithmetic Progression Sequence: -2, 0, 2, 4, with labels indicating the first term (a) and the common difference (d).

What is an Arithmetic Progression (AP)?

An Arithmetic Progression (AP) is a sequence of numbers where the difference between any two consecutive terms is always the same. This difference is called the common difference.

For example, in the sequence: 2, 5, 8, 11, 14...

The difference between each number is 3. So, the common difference is 3.

Formula to Find the nth Term

Diagram visualizing the nth term calculation in an arithmetic progression sequence, with terms labeled 1 to 5 and corresponding values 2, 5, 8, 11, 14. The formula for the nth term, a + (n-1) * d, is shown.
Visualizing nth Term Calculation in Arithmetic Progression: A formula for the nth term, a + (n-1) * d, applied to terms 1 through 5 with corresponding values.

So, here’s the formula for finding the nth term of an arithmetic progression (AP):

an​=a+(n−1)×d

Let me explain each part:

  • a: This is the first term of the sequence. For example, in the sequence 2, 5, 8, 11, 14..., the first term (a) is 2.
  • d: This is the common difference, or the number you keep adding to get from one term to the next. In our example (2, 5, 8, 11, 14...), the common difference (d) is 3 because each number increases by 3.
  • n: This is the term number. If you want the 5th term, then n = 5. If you want the 100th term, then n = 100.

How to Use It:

Let’s say you want to find the 7th term in the sequence 2, 5, 8, 11, 14.... Here’s how you can use the formula:

  1. The first term (a) is 2.
  2. The common difference (d) is 3.
  3. You want the 7th term, so n = 7.

Now, plug these into the formula:

a7=2+(7−1)×3

a7​=2+6×3

a7​=2+18

a7​=20

So, the 7th term of the sequence is 20.

See how easy it is? You just plug in the values for a, d, and n into the formula, and you get the term you want.

Python Program to Find the nth Term of an Arithmetic Progression

Now that you know the formula, let’s write a simple Python program to find the nth term:

def find_nth_term(a, d, n):
    return a + (n - 1) * d

# Example
first_term = 2
common_difference = 3
term_number = 5

nth_term = find_nth_term(first_term, common_difference, term_number)
print(f"The {term_number}th term is {nth_term}")

Step-by-Step Explanation of the Code

Let’s go through it line by line:

  • def find_nth_term(a, d, n):
    This defines a function called find_nth_term that takes three inputs:
    • a (first term)
    • d (common difference)
    • n (term number you want to find)
  • return a + (n - 1) * d
    Inside the function, we use the formula we learned earlier to calculate and return the nth term.
  • first_term = 2
    We set the first term of our sequence to 2.
  • common_difference = 3
    The common difference between each term is 3.
  • term_number = 5
    We want to find the 5th term.
  • nth_term = find_nth_term(first_term, common_difference, term_number)
    We call the function find_nth_term and pass in our values. It gives us back the 5th term.
  • print(f"The {term_number}th term is {nth_term}")
    Finally, we print out the result in a friendly sentence.

Output

When you run this program, you’ll see:

The 5th term is 14

Python Program with User Input

Let’s make it a little more interactive so that you (or anyone using your program) can enter the values.

Here’s the updated version:

def find_nth_term(a, d, n):
    return a + (n - 1) * d

# Asking the user for input
first_term = int(input("Enter the first term (a): "))
common_difference = int(input("Enter the common difference (d): "))
term_number = int(input("Enter the term number (n): "))

# Finding the nth term
nth_term = find_nth_term(first_term, common_difference, term_number)

# Printing the result
print(f"The {term_number}th term is {nth_term}")

Step-by-Step Explanation of the New Code

  • input() is used to ask the user for the first term, common difference, and term number.
  • int() is used to convert whatever the user types (which is always a string) into a number (integer).
  • Then the rest of the program works the same: we find the nth term using the formula and print the result.

Run

If you run the program and type:

Enter the first term (a): 2
Enter the common difference (d): 3
Enter the term number (n): 5

You will get:

The 5th term is 14

Must Read


Python Program with Error Handling

Let’s make our program a little smarter so it doesn’t crash if someone types something wrong (like letters instead of numbers).

We’ll use try-except in Python. Here’s the improved version:

def find_nth_term(a, d, n):
    return a + (n - 1) * d

try:
    # Asking the user for input
    first_term = int(input("Enter the first term (a): "))
    common_difference = int(input("Enter the common difference (d): "))
    term_number = int(input("Enter the term number (n): "))

    # Finding the nth term
    nth_term = find_nth_term(first_term, common_difference, term_number)

    # Printing the result
    print(f"The {term_number}th term is {nth_term}")

except ValueError:
    print("Oops! Please enter only numbers.")

Step-by-Step Explanation

  • try: tells Python: “Try running this block of code.”
  • If the user enters something wrong (like typing “abc” instead of a number), Python jumps to the except ValueError: part.
  • except ValueError: catches that mistake and shows a friendly message:
    "Oops! Please enter only numbers."
  • This way, your program doesn’t crash and stays user-friendly.

Example

If the user types:

Enter the first term (a): two

The output will be:

Oops! Please enter only numbers.

Real-World Use Cases of an Arithmetic Progression in Python

You might be wondering — where would I even use finding the nth term in real life?
Well, it actually comes up more often than you’d think!

Here are some places where it’s super useful:

Math Problem Solving:

If you’re solving math puzzles or competitive exam questions, finding the nth term helps you quickly get the answer without writing out the whole sequence.

Game Development (Level Progressions):

Suppose you’re making a game, and you want each level to get a little harder than the last one. Maybe the number of enemies increases by 5 every level. You could use an AP formula to figure out how many enemies are on level 20 without manually calculating each one.

Mini Example: Finding Enemies at a Game Level

Story:
You are building a game where the number of enemies increases by 5 for each level.
The first level has 10 enemies.
You want to find out how many enemies will be there at Level 12.

Here’s the Python code:

def find_nth_term(a, d, n):
    return a + (n - 1) * d

# First term (Level 1 enemies)
first_level_enemies = 10

# Common difference (Enemies increase by 5 each level)
increase_per_level = 5

# Level number you want to check
level_number = 12

# Finding enemies at that level
enemies_at_level = find_nth_term(first_level_enemies, increase_per_level, level_number)

print(f"There will be {enemies_at_level} enemies at Level {level_number}.")

Output

There will be 65 enemies at Level 12.

Quick Explanation

  • First level starts with 10 enemies.
  • Each new level adds 5 more enemies.
  • So at Level 12, there will be 65 enemies.

And boom! You just used an arithmetic progression to make your game smarter!

Financial Calculations (Like Loan Payments or Savings):

In finance, you might have things like monthly loan payments or savings plans where the amount increases by a fixed number every month.
Finding the nth term helps you know how much you’ll owe or have saved after a certain number of months.

Mini Example: Growing Monthly Savings

Diagram showing the growth of savings over time with a starting amount of $100 and an increase of $20 each month, represented as an arithmetic progression.
Real-World Application of Arithmetic Progression: Tracking the growth of savings with an initial deposit of $100 and a $20 monthly increase.

Story:
Suppose you decide to save money every month.

  • You start by saving $100 in the first month.
  • Every month, you add $20 more than the previous month.
  • You want to know how much you will save in the 10th month.

Here’s the Python code:

def find_nth_term(a, d, n):
    return a + (n - 1) * d

# First month savings
first_month_savings = 100

# Monthly increase
monthly_increase = 20

# Month number you want to check
month_number = 10

# Finding savings for that month
savings_in_month = find_nth_term(first_month_savings, monthly_increase, month_number)

print(f"You will save ${savings_in_month} in month {month_number}.")

Output

You will save $280 in month 10.

Quick Explanation

  • You save $100 in the first month.
  • Each month you add $20 more than last month.
  • By the 10th month, you’re saving $280 in that month!

Conclusion

And that’s it!
Now you know what an Arithmetic Progression (AP) is, how to find the nth term, and how to code it easily in Python.

We even looked at real-world examples like game development and financial savings — so you can see that AP isn’t just a boring math topic, it’s actually super useful in real life too!

Whether you’re solving a math puzzle, designing a game, or planning your finances, knowing how to find the nth term can save you a lot of time and effort.

If you enjoyed this simple guide, feel free to try changing the numbers and build your own mini-projects!
The more you practice, the more comfortable you’ll get.

Thanks for reading!

Quiz: Test Your Knowledge on Arithmetic Progressions (AP) in Python!

1. What is the formula to find the nth term of an Arithmetic Progression (AP)?
a) a_n = a + (n – 1) ×d
b) a_n = a + (n + 1) ×d
c) a_n = a ×d
d)a_n = a – (n – 1) ×d

Answer: a) an=a+(n−1)×d

Explanation:
This is the standard formula for the nth term of an arithmetic progression. a is the first term, d is the common difference, and n is the term number.

If the first term of an AP is 5 and the common difference is 3, what is the 4th term?
a) 11
b) 12
c) 13
d) 14

Answer: a) 11

Explanation:
We can apply the formula:
an=a+(n−1)×d
For the 4th term:
a4=5+(4−1)×3=5+9=11

3. Which of the following is a real-world example of using Arithmetic Progression?
a) Predicting a person’s age based on their birth year
b) Calculating monthly savings where the savings increase by a fixed amount every month
c) Finding the fastest route to a destination
d) Computing the temperature of a day based on time of the year

Answer: b) Calculating monthly savings where the savings increase by a fixed amount every month

Explanation:
This is a perfect example of an arithmetic progression because your savings grow by a fixed amount (common difference) every month.

4. In the Python code, what does the function find_nth_term(a, d, n) calculate?
a) The sum of the first n terms of the AP
b) The nth term of the AP
c) The common difference between terms
d) The first term of the AP

Answer: b) The nth term of the AP

Explanation:
The function find_nth_term(a, d, n) calculates the nth term of the arithmetic progression using the formula an=a+(n−1)×da_n = a + (n – 1) \times dan​=a+(n−1)×d.

5. In a game where enemies increase by 5 each level, if you start with 10 enemies at level 1, how many enemies will there be at level 8?
a) 35
b) 40
c) 45
d) 50

Answer: b) 40

Explanation:
Using the formula:
an=a+(n−1)×d
For level 8:
a8=10+(8−1)×5=10+35=40

External Resources

Khan Academy – Arithmetic Sequences and Series

Khan Academy provides a comprehensive, beginner-friendly course on arithmetic sequences and series, where you can explore the formulas and applications step-by-step. This resource is great for both beginners and those looking to refresh their knowledge.

Brilliant.org – Sequences and Series

Brilliant offers a more interactive learning approach, with problems and explanations that help you solve problems related to arithmetic progressions. Their approach is more engaging and visual, helping you think through each concept.

Coursera – Introduction to Mathematical Thinking

This course offers an in-depth look at mathematical thinking, including topics like sequences and series. It’s a great resource if you want to understand the theory behind arithmetic progressions before applying them in code.

Python.org – Python Documentation

For those new to Python or looking for official resources, the Python.org documentation offers detailed explanations of functions, classes, and libraries that you can use to implement arithmetic progressions in Python.

FAQs on Arithmetic Progression (AP) in Python

1. What is an Arithmetic Progression (AP)?

An Arithmetic Progression (AP) is a sequence of numbers where the difference between any two consecutive terms is constant. For example, in the sequence 2, 5, 8, 11, the common difference is 3.

2. How do I find the nth term of an AP?

To find the nth term of an AP, use the formula:
a_n = a + (n – 1)×d
where a is the first term, d is the common difference, and n is the term number.

3. What are some real-world applications of APs?

APs are used in real-world scenarios like calculating monthly savings, loan payments, and game level progressions, where the increase or decrease between consecutive terms is constant.

4. Can I use Python to calculate the nth term of an AP?

Yes, you can use Python to calculate the nth term of an AP. Simply define the first term, common difference, and term number, and apply the formula within a Python function.

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Rating