human
Give me a code to sum of elements of a Geometric Progression ( GP ) in a given range and the other steps required given the following:
Sum of elements of a Geometric Progression ( GP ) in a given range | Function to find sum in the given range.
Find the value of k.
Find the common difference.
Find the sum.
Driver code"
Solution in Python:
assistant
#Here is code to sum of elements of a Geometric Progression ( GP ) in a given range and the other steps required.
 def findSum ( arr , n , left , right ) :
	k = right - left + 1
	d = arr [ 1 ] // arr [ 0 ]
	ans = arr [ left - 1 ]
	if d == 1 :
		ans = ans * d * k
	else :
		ans = ans * ( d ** k - 1 ) // ( d - 1 )
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 ]
	queries = 3
	q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 8 ] ]
	n = len ( arr )
	for i in range ( queries ) :
		print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) )