Given n, a and r as the number of terms, first term and common ratio respectively of an Geometric Progression Series. Find the sum of the series upto nth term.
 
Example 1:
Input: 3 3 2
Output: 21
Explanation: Series upto 3td term is
3 6 12, so sum will be 21.
Example 2:
Input: 3 1 2
Output: 7
Example: Series upto 3rd term is 
1 2 4, so sum will be 7.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function sum_of_gp() which takes n, a and r as input parameter and returns the sum of the series.
 
Expected Time Complexity: O(1)
Expected Space Complexity: O(1)
 
Constranits:
1 <= n, a, r <= 10

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:
	def sum_of_gp(self, n, a, r):
		# Code here

Solutions:
class Solution:

	def sum_of_gp(self, n, a, r):
		if r == 1:
			return int(a * n)
		return int((a * r ** n - a) / (r - 1))


import math

class Solution:

	def sum_of_gp(self, n, a, r):
		if r != 1:
			return int(a * (1 - math.pow(r, n)) / (1 - r))
		else:
			return a * n


class Solution:

	def sum_of_gp(self, n, a, r):
		sum = 0
		if r > 1:
			sum = int(a * (1 - r ** n) / (1 - r))
			return sum
		elif r == 1:
			return a * n


class Solution:

	def sum_of_gp(self, n, a, r):
		suma = 0
		for i in range(0, n):
			suma += a * pow(r, i)
		return suma


class Solution:

	def sum_of_gp(self, n, a, r):
		if r == 1:
			return n * a
		else:
			return a * (r ** n - 1) // (r - 1)


from math import sqrt

class Solution:

	def sum_of_gp(self, n, a, r):
		if r != 1:
			h = a * (1 - pow(r, n)) / (1 - r)
			return int(h)
		else:
			return n * a


class Solution:

	def sum_of_gp(self, n, a, r):
		if r == 1:
			s = n * a
		elif r > 1:
			s = a * (r ** n - 1) / (r - 1)
		else:
			s = a * (1 - r ** n) / (1 - r)
		return int(s)


import math

class Solution:

	def sum_of_gp(self, n, a, r):
		ln = [a]
		for i in range(1, n):
			ln.append(a * r ** i)
		return sum(ln)


class Solution:

	def sum_of_gp(self, n, a, r):
		total = 0
		term = a
		for i in range(n):
			total += term
			term *= r
		return total


class Solution:

	def sum_of_gp(self, n, a, r):
		N = a * (pow(r, n) - 1)
		D = r - 1
		if r == 1:
			return n * a
		return int(N / D)


class Solution:

	def sum_of_gp(self, n, a, r):
		try:
			sum = a * ((1 - r ** n) / (1 - r))
			return int(sum)
		except:
			return a * n


class Solution:

	def sum_of_gp(self, n, a, r):
		l = [a]
		temp = a
		for i in range(n - 1):
			temp = temp * r
			l.append(temp)
		return sum(l)


class Solution:

	def sum_of_gp(self, n, a, r):
		if r != 1:
			return a * (pow(r, n) - 1) // (r - 1)
		return a * n


class Solution:

	def sum_of_gp(self, n, a, r):
		if r == 1:
			return a * n
		return round(a * ((1 - r ** n) / (1 - r)))


class Solution:

	def sum_of_gp(self, n, a, r):
		sum = a
		for i in range(n - 1):
			sum = sum + a * r
			a = a * r
		return sum


class Solution:

	def sum_of_gp(self, n, a, r):
		c = [a]
		for ele in range(n - 1):
			c.append(c[ele] * r)
		return sum(c)


class Solution:

	def sum_of_gp(self, n, a, r):
		if r != 1:
			return int(a * abs(1 - pow(r, n)) / abs(1 - r))
		else:
			return int(a * n)


class Solution:

	def sum_of_gp(self, n, a, r):
		if r == 1:
			return a * n
		ans = a * ((r ** n - 1) // (r - 1))
		return ans


class Solution:

	def sum_of_gp(self, n, a, r):
		z = a * (1 - r ** n)
		x = 1 - r
		if r > 1:
			return int(z / x)
		else:
			return int(a * n)


class Solution:

	def sum_of_gp(self, n, a, r):
		l = []
		for i in range(0, n):
			curr_term = a * pow(r, i)
			l.append(curr_term)
		return sum(l)