Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-147

Geometric Series Sum

Problem

Given first term a, integer ratio r and a count n, return [nth term, sum of the first n terms].

The nth term is a * r ** (n - 1); for r != 1 the sum is a * (r ** n - 1) // (r - 1), and for r == 1 it is a * n. Assume n >= 1 and integer arithmetic.

Hint

The closed form divides exactly for integer geometric series with r != 1.

Test cases

case 1
in: [1,2,4]
expected: [8,15]
case 2
in: [2,3,3]
expected: [18,26]
case 3
in: [5,1,4]
expected: [5,20]
case 4
in: [0,7,5]
expected: [0,0]
case 5
in: [3,-1,4]
expected: [-3,0]
Starter codePython
def geometric_series(a, r, n):
    # Your code here
    pass

5 test cases · run your code against them in the editor