Learn Math With Python

11. Learn Math With Python – Arithmetic Progression

A sequence of numbers where each term differs from its previous term by a constant quantity is called an arithmetic progression. In arithmetic progression (AP), the first term is denoted by a and the common difference is denoted by d.

Therefore, an arithmetic progression can be represented as a, a + d, a + 2d, a + 3d, …., 

nth term of an AP

In an AP,

1st term = a = a + 0 × d = a + (1 – 1) × d

2nd term = a + d = a + 1 × d = a + (2 – 1) × d

3rd term = a + 2d = a + 2 × d = a + (3 – 1) × d

4th term = a + 3d = a + 3 × d = a + (4 – 1) × d

Moving on in this fashion, we get

nth term = a + (n – 1)d 

Sum of n terms of an arithmetic progression = Sn = a + (a + d) + (a + 2d) + (a + 3d) + … + (a + (n – 1)d) ——— (1)

Writing (1) in reverse order, we get Sn = (a + (n – 1)d) + (a + (n – 2)d) + (a + (n – 2)d) + (a + (n – 3)d) + … + a —— (2)

Adding (1) and (2)

2Sn = (2a + (n – 1)d) + (2a + (n – 1)d) + (2a + (n – 1)d) + … (n times)

=>2Sn = n × (2a + (n – 1)d)=>Sn = (n/2) × (2a + (n – 1)d)

11.1 Printing an AP

You can print terms of an AP and its sum by using the above formuale.

#Arithmetic Progression
a = 0
d = 0
n = 0
an = 0
sn = 0

#asking for input  
a = float(input('Enter first term of an AP '))
d = float(input('Enter common difference of an AP '))
n = int(input('Enter number of terms in an AP '))

#Calculating AP  
print('AP is')
for i in range(1, n):
  an = a + (i - 1) * d
  if i == 1:
	  print(i, 'st term = ', an)
  elif i == 2:   
    print(i, 'nd term = ', an)
  elif i == 3:
    print(i, 'rd term = ', an)
  else:
    print(i, 'th term = ', an)

sn = (n/2) * ((2 * a) + (n - 1) * d) 
print('Sum upto ', n, 'terms of an AP is', sn)

11.2 Printing a Portion of an AP

Instead of printing an AP from the first term to the last term, you can print a portion of it.

#Portion of an Arithmetic Progression
a = 0
d = 0
tn = 0
n1 = 0
n2 = 0
an1 = 0
an2 = 0
sn1 = 0
sn2 = 0
sn = 0
n_bet = 0

#asking for input  
a = float(input('Enter first term of an AP '))
d = float(input('Enter common difference of an AP '))
n = int(input('Enter number of terms in an AP '))

#Printing Portion of an AP
n1 = int(input('Print AP from (term) '))
n2 = int(input('Print AP upto (term) '))

#Calculating AP
an1 = a + (n1 - 1) * d
an2 = a + (n2 - 1) * d
n_bet = ((an2 - an1)/d + 1)

print('Number of terms from ', n1, 'th term to ', n2, 'th term is ', int(n_bet))

print('AP between ', n1, 'th term and', n2, 'th term is ')
for i in range(n1, n2 + 1):
	an = a + (i - 1) * d
	print(i, 'th term = ', an)

sn = ((n_bet/2) * ((2 * an1) + ((an2 - an1)/d + 1) - 1) * d)
print('Sum of AP between', n1, 'th term and', n2, 'th term is', sn)

Conclusion

Writing mathematics in Python is an easy and effective way of using python programming for your daily needs. Hope this article on Python will guide you step by step on how to write math formulae and functions with Python.

Recommended Reading

Image Credit: Online work vector created by svstudioart – www.freepik.com

Leave a Comment