Menu Close

Find the missing element in geometric progression GP

Given that represents elements of geometric progression in order.One element is missing in progression, find the missing number. It may be assumed that one term is always missing and the missing term is not first or last of series.
Examples:
Input:
        {1,3,27,81}
Output:
        9

Input:
        {4,16,64,1024}
Output:
        256

The nth term of a GP series is Tn = arn-1, where a = first term and   r = common ratio  =  ( Tn/Tn-1) . 
GP formula in python:
                                     a*(r^(n-1))
                                                        which is arn-1

A = list(map(int,input("Enter the GP sequence numbers: ").split())) #map returns the object. It passes elements from 2nd arguement to 1st.
N=len(A)
if A[0]!=1:
	a=A[0]
	if A[1]/A[0] == A[2]/A[1]:
		r=A[1]/A[0]
		for i in range(1,N+1):
			X=int(a*(r**(i-1))) #Geometric progression formula a*(r^(n-1))
			if X!=A[i-1]:
				print("The missing number from GP sequence is ",X)
				break

	else:
		print("It is not GP Sequence")
else:
	a=A[1]
	if A[1]%A[0] == A[2]%A[1]:
		r=A[1]/A[0]
		for i in range(2,N+1):
			X=int(a*(r**(i-1)))
			if X!=A[i-1]:
				print("The missing number from GP sequence is ",X)
				break

	else:
		print("It is not GP Sequence")

Exc. on terminal

More Codes to Fcuk