Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-146

Arithmetic Progression

Problem

Given first term a, common difference d and a count n, return [nth term, sum of the first n terms].

The nth term is a + (n - 1) * d and the sum is n * (2 * a + (n - 1) * d) // 2. Assume n >= 1; a and d may be negative or zero.

Hint

The sum pairs the first and last terms, so it is n times their average.

Test cases

case 1
in: [2,3,5]
expected: [14,40]
case 2
in: [1,1,10]
expected: [10,55]
case 3
in: [0,0,1]
expected: [0,0]
case 4
in: [5,-2,4]
expected: [-1,8]
case 5
in: [10,5,1]
expected: [10,10]
Starter codePython
def arithmetic_progression(a, d, n):
    # Your code here
    pass

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