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 is looks like a, a + d, a + 2d, a + 3d, . . . Task is to find the sum of series.
Examples:
Input : a = 1
d = 2
n = 4
Output : 16
1 + 3 + 5 + 7 = 16
Input : a = 2.5
d = 1.5
n = 20
Output : 335
A simple solution to find sum of arithmetic series.
C++
#include<bits/stdc++.h>
using namespace std;
float sumOfAP(float a, float d, int n)
{
float sum = 0;
for (int i=0;i<n;i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
int main()
{
int n = 20;
float a = 2.5, d = 1.5;
cout<<sumOfAP(a, d, n);
return 0;
}
|
Java
class GFG{
static float sumOfAP(float a, float d,
int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
public static void main(String args[])
{
int n = 20;
float a = 2.5f, d = 1.5f;
System.out.println(sumOfAP(a, d, n));
}
}
|
Python
def sumOfAP( a, d,n) :
sum = 0
i = 0
while i < n :
sum = sum + a
a = a + d
i = i + 1
return sum
n = 20
a = 2.5
d = 1.5
print (sumOfAP(a, d, n))
|
C#
using System;
class GFG {
static float sumOfAP(float a, float d,
int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
public static void Main()
{
int n = 20;
float a = 2.5f, d = 1.5f;
Console.Write(sumOfAP(a, d, n));
}
}
|
PHP
<?php
function sumOfAP($a, $d, $n)
{
$sum = 0;
for ($i = 0; $i < $n; $i++)
{
$sum = $sum + $a;
$a = $a + $d;
}
return $sum;
}
$n = 20;
$a = 2.5; $d = 1.5;
echo(sumOfAP($a, $d, $n));
?>
|
Javascript
<script>
function sumOfAP(a, d, n)
{
let sum = 0;
for (let i=0;i<n;i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
let n = 20;
let a = 2.5, d = 1.5;
document.write(sumOfAP(a, d, n));
</script>
|
Output:
335
Time Complexity: O(n)
An Efficient solution to find the sum of arithmetic series is to use below formula.
Sum of arithmetic series
= ((n / 2) * (2 * a + (n - 1) * d))
Where
a - First term
d - Common difference
n - No of terms
C++
#include<bits/stdc++.h>
using namespace std;
float sumOfAP(float a, float d, float n)
{
float sum = (n / 2) * (2 * a + (n - 1) * d);
return sum;
}
int main()
{
float n = 20;
float a = 2.5, d = 1.5;
cout<<sumOfAP(a, d, n);
return 0;
}
|
Java
class GFG
{
static float sumOfAP(float a, float d, float n)
{
float sum = (n / 2) * (2 * a + (n - 1) * d);
return sum;
}
public static void main (String[] args)
{
float n = 20;
float a = 2.5f, d = 1.5f;
System.out.print(sumOfAP(a, d, n));
}
}
|
Python3
def sumOfAP(a, d, n):
sum = (n / 2) * (2 * a + (n - 1) * d)
return sum
n = 20
a = 2.5
d = 1.5
print(sumOfAP(a, d, n))
|
C#
using System;
class GFG {
static float sumOfAP(float a,
float d,
float n)
{
float sum = (n / 2) *
(2 * a +
(n - 1) * d);
return sum;
}
static public void Main ()
{
float n = 20;
float a = 2.5f, d = 1.5f;
Console.WriteLine(sumOfAP(a, d, n));
}
}
|
PHP
<?php
function sumOfAP($a, $d, $n)
{
$sum = ($n / 2) * (2 * $a +
($n - 1) * $d);
return $sum;
}
$n = 20;
$a = 2.5; $d = 1.5;
echo(sumOfAP($a, $d, $n));
?>
|
Javascript
function sumOfAP(a, d, n) {
let sum = (n / 2) * (2 * a + (n - 1) * d);
return sum;
}
let n = 20;
let a = 2.5, d = 1.5;
document.write(sumOfAP(a, d, n));
|
Time Complexity: O(1)
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 geometric series is
= (((k-1))/ 2) * (2 * a + (k - 2) * d))
We know k-th term of arithmetic series is
= a + (k - 1)*d
Sum of first k elements =
= Sum of (k-1) numbers + k-th element
= (((k-1)/2)*(2*a + (k-2)*d)) + (a + (k-1)*d)
= [((k-1)(2a + (k-2)d) + (2a + 2kd - 2d)]/2
= ((k / 2) * (2 * a + (k - 1) * d))This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.