A series with same common difference is known as arithmetic series. The first term of series is 'a' and common difference is d. The series looks like a, a + d, a + 2d, a + 3d, . . . Find the sum of series upto nth term.
Examples:
Input: n = 5, a = 1, d = 3
Output: 35
Explanation: Series upto 5th term is1 4 7 10 13, so sum will be 35.Input: n = 3, a = 1, d = 2
Output: 9
Example: Series upto 3rd term is 1 3 5, so sum will be 9.
Table of Content
[Naive Approach] Using Iteration – O(n) Time and O(1) Space
The idea is to generate each term of the Arithmetic Progression one by one and keep adding it to the final sum. Starting from the first term, we repeatedly add the common difference to obtain the next term until all
nterms are processed.
- Initialize the sum as
0 - Iterate
ntimes to generate all AP terms - Add the current term to the sum and update it using common difference
- Return the final accumulated sum
#include <iostream>
using namespace std;
// Function to find sum of series.
int sumOfAP(int n, int a, int d)
{
int sum = 0;
int term = a;
for (int i = 0; i < n; i++)
{
sum = sum + term;
term = term + d;
}
return sum;
}
// Driver function
int main()
{
int n, a, d;
cin >> n >> a >> d;
cout << sumOfAP(n, a, d);
return 0;
}
// Java program to find sum of Arithmetic Progression
import java.util.*;
class GfG {
// Function to find sum of series
static int sumOfAP(int n, int a, int d) {
int sum = 0;
int term = a;
for (int i = 0; i < n; i++) {
sum = sum + term;
term = term + d;
}
return sum;
}
// Driver function
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = sc.nextInt();
int d = sc.nextInt();
System.out.println(sumOfAP(n, a, d));
sc.close();
}
}
# Python program to find sum of Arithmetic Progression
# Function to find sum of series
def sumOfAP(n, a, d):
sum_val = 0
term = a
for i in range(n):
sum_val = sum_val + term
term = term + d
return sum_val
# Driver function
if __name__ == "__main__":
n, a, d = map(int, input().split())
print(sumOfAP(n, a, d))
// C# program to find sum of Arithmetic Progression
using System;
class GfG {
// Function to find sum of series
static int sumOfAP(int n, int a, int d) {
int sum = 0;
int term = a;
for (int i = 0; i < n; i++) {
sum = sum + term;
term = term + d;
}
return sum;
}
// Driver function
static void Main(string[] args) {
string[] input = Console.ReadLine().Split();
int n = int.Parse(input[0]);
int a = int.Parse(input[1]);
int d = int.Parse(input[2]);
Console.WriteLine(sumOfAP(n, a, d));
}
}
// JavaScript program to find sum of Arithmetic Progression
// Function to find sum of series
function sumOfAP(n, a, d) {
let sum = 0;
let term = a;
for (let i = 0; i < n; i++) {
sum = sum + term;
term = term + d;
}
return sum;
}
// Driver function
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('', (input) => {
const [n, a, d] = input.split(' ').map(Number);
console.log(sumOfAP(n, a, d));
rl.close();
});
Output
230
[Optimal Approach] Using Arithmetic Progression Formula – O(1) Time and O(1) Space
The idea is to directly use the mathematical formula for the sum of an Arithmetic Progression instead of generating each term individually. Since the first term, common difference, and number of terms are known, the sum can be computed in constant time using the AP sum formula.
Sum of arithmetic series = ((n / 2) * (2 * a + (n - 1) * d))
Where: a - First term, d - Common difference, n - No of terms
How does this formula work?
We can prove the formula using mathematical induction. We can easily see that the formula holds true for n = 1 and n = 2. Let this be true for n = k-1.
Let the formula be true for n = k - 1.
Sum of first (k - 1) elements of arithmetic series: = ((k - 1) / 2) × [2a + (k - 2)d]We know the k-th term of arithmetic series is: Tk = a + (k - 1)d
Sum of first k elements:= Sum of first (k - 1) elements + k-th element
= [((k - 1) / 2) × (2a + (k - 2)d)] + [a + (k - 1)d]
= { (k - 1)(2a + (k - 2)d) + 2a + 2(k - 1)d } / 2
= { 2ak - 2a + k²d - 3kd + 2d + 2a + 2kd - 2d } / 2
= { 2ak + k²d - kd } / 2
= { k(2a + (k - 1)d) } / 2
= (k / 2) × [2a + (k - 1)d]
Example:
#include <iostream>
using namespace std;
int sumOfAP(int a, int d, int n)
{
int sum = (n * (2 * a + (n - 1) * d)) / 2;
return sum;
}
// Driver code
int main()
{
int n = 20;
int a = 2, d = 1;
cout << sumOfAP(a, d, n);
return 0;
}
import java.util.*;
class GFG {
static int sumOfAP(int a, int d, int n)
{
int sum = (n * (2 * a + (n - 1) * d)) / 2;
return sum;
}
// Driver code
public static void main(String[] args)
{
int n = 20;
int a = 2, d = 1;
System.out.println(sumOfAP(a, d, n));
}
}
def sumOfAP(a, d, n):
sum = (n * (2 * a + (n - 1) * d)) // 2
return sum
# Driver code
if __name__ == "__main__":
n = 20
a = 2
d = 1
print(sumOfAP(a, d, n))
using System;
class GFG {
static int sumOfAP(int a, int d, int n)
{
int sum = (n * (2 * a + (n - 1) * d)) / 2;
return sum;
}
// Driver code
static void Main()
{
int n = 20;
int a = 2, d = 1;
Console.WriteLine(sumOfAP(a, d, n));
}
}
function sumOfAP(a, d, n)
{
let sum = (n * (2 * a + (n - 1) * d)) / 2;
return sum;
}
// Driver code
let n = 20;
let a = 2, d = 1;
console.log(sumOfAP(a, d, n));
Output
230