First N Terms of GP

Print first n terms of geometric progression (a, r).

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 10 of 13
first-n-terms-of-gp.py
1# Take inputs
2a = int(input("Enter first term (a): "))
3r = int(input("Enter common ratio (r): "))
4n = int(input("Enter number of terms (n): "))
5
6# Print GP
7print(f"First {n} terms of GP:")
8for i in range(n):
9 term = a * (r ** i)
10 print(term, end=" ")
11print()

Output

Enter first term (a): 2
Enter common ratio (r): 3
Enter number of terms (n): 5
First 5 terms of GP:
2 6 18 54 162

What's going on

GP: each term = first term * (ratio ^ (position - 1)).

Key Concepts:

nth term = a * (r ^ (n-1))
Loop from 0 to n-1
Calculate each term using power