Arithmetic Progression through Python
What is Arithmetic progression?
A sequence a1
, a2
, a3
,…, an
,… is called arithmetic sequence or arithmetic
progression if an + 1 = an
+ d, n ∈ N, where a1
is called the first term and the constant
term d is called the common difference of the A.P.
Let us consider an A.P. (in its standard form) with first term a and common
difference d, i.e., a, a + d, a + 2d, ...
Then the n
th term (general term) of the A.P. is an
= a + (n – 1) d.
We can verify the following simple properties of an A.P. :
(i) If a constant is added to each term of an A.P., the resulting sequence is
also an A.P.
(ii) If a constant is subtracted from each term of an A.P., the resulting
sequence is also an A.P.
(iii) If each term of an A.P. is multiplied by a constant, then the resulting
sequence is also an A.P.
(iv) If each term of an A.P. is divided by a non-zero constant then the
resulting sequence is also an A.P.
Here, we shall use the following notations for an arithmetic progression:
a = the first term, l = the last term, d = common difference,
n = the number of terms.
Sn
= the sum to n terms of A.P.
Let a, a + d, a + 2d, …, a + (n – 1) d be an A.P. Then
l = a + (n – 1) d
Let's understand AP in simple example ,
1+2+3+4+ .... +50 (Sum of sequence Natural number )
or
0+2+4+6+ .... +48 (Sum of sequence of Even number)
or
1+3+5+7 +.....+49 (sum of sequence of Odd number )
If you look at above three sequences ,
- First sequence is sum of natural number . between 1 and 2 , 2 and 3 etc, there difference is 1 .
- Second sequence is sum of even number . There common difference is 2 .
- In third sequence , the common difference between the number is also 2 .
Algorithm :
- If You want to calculate sum of a to b range , your input will be a b c you get following answers :
and
0+2+4+6+8+10+.....+50 = Result (Even number summation)
and
1+3+5+7+9+11+....+49 = Result (Odd number summation)
Where a= starting point (for above case, a=0) , b = Stopping point( for above case ,b=50) and for c = step size(if step size is 2 , this program will take 0,2,4,6,.....,50)(for above case c=0)
If c not equal to zero , it only print the summation of specific number range with specific step .
Source code :
a=0
a1=0
x,y,z = [int(f) for f in input("Enter the number range to do summation (START STOP STEP):").split()]
if z==0 :
for i in range(x,y+1):
if (i % 2 ==0):
a += i
else:
a1 += i
print(f"Sum of even number between {x} to {y} range :",a)
print(f"Sum of odd number between {x} to {y} range :",a1)
print(f"Sum of whole number between {x} to {y} range :",a1+a)
else :
for i in range(x,y+1,z):
a += i
print(f"Sum of the numbers between {x} to {y} range with step {z} :",a)
Output: >>>Enter the number range to do summation (START STOP STEP):0 50 2
>>>Sum of the numbers between 0 to 50 range with step 2 : 650
Another >>>Enter the number range to do summation (START STOP STEP):0 50 0
>>>Sum of even number between 0 to 50 range : 650
Sum of odd number between 0 to 50 range : 625
Sum of whole number between 0 to 50 range : 1275
Share: