Series and Progression – Arithmetic, Geometric and Harmonic

Let us clarify few terms first.

A sequence is a set of numbers written in a particular order.

A series is something we obtain from a sequence by adding all the terms together. Please note its not sequence of numbers, its sum of numbers in sequence.

progression has a specific formula to calculate its nth term, whereas a sequence can be based on a logical rule like ‘a group of prime numbers

Here is an example of sequence

u1,u2,u3,.....,un

 

Example of Series

u1+u2+u3+.....+un

Arithmetic Series

Arithmetic sequence is a sequence of numbers in which each term after the first is obtained by adding a constant (d).

An arithmetic progression is given by following formula

a,  (a + d), (a + 2d), (a + 3d), ....(a+(n1)d)

 

where a = the first term , d = the common difference

Some of the important formulae

To find nth Term: an = a + (n  1)d To find lenght : n = ( last term  first term )d  + 1To find Sum till nth Element : Sn = 12n(first term + last term)

Geometric Progression

A geometric progression, or GP, is a sequence where each new term after the first is obtained by multiplying the preceding term by a constant r.

a, ar , ar2 , ar3 , . ..,ar(n1)

 

Where a : first term and r is constant

To find nth Term : an = ar(n  1)To find Sum till nth Element : Sn = a(1rn)(1r)

Harmonic Progression

harmonic progression is closely related with arithmetic progression. Non-zero numbers

a1,a2,a3,...,an

 

are in Harmonic Progression(HP) if

1a1,1a2,1a3,...,1an

are in Arithmetic progression

Python program to identify type of progression


################################################################################################
#	name:	progression.py
#	desc:	identify type of progression
#	date:	2018-09-08
#	Author:	conquistadorjd
################################################################################################

import numpy

def type_of_progression(input_sequence):

	delta = []
	current = 0
	while current < len(input_sequence)-1:

		delta.append(input_sequence[current+1]-input_sequence[current])
		current = current+1

	delta_unique= set(delta)
	if len(delta_unique) == 1 :
		return "arithmetic"
	
	delta = []
	current = 0
	while current < len(input_sequence)-1:

		delta.append(input_sequence[current+1]/input_sequence[current])
		current = current+1

	delta_unique= set(delta)
	if len(delta_unique) == 1 :
		return "geometric"

	delta = []
	current = 0
	while current < len(input_sequence)-1:

		delta.append(1/input_sequence[current+1]-1/input_sequence[current])
		current = current+1

	delta_unique= set(delta)
	if len(delta_unique) == 1 :
		return "harmonic"
	else:
		return "nothing"

print('*** Program Started ***')

input_sequence = input('Please input sequence separated by "," : ')
input_sequence = eval('[' + input_sequence + ']')


result = type_of_progression(input_sequence)
print("result :" , result)

print('*** Program Ended ***')