Given a positive integer n and the task is to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n.
Examples:
Input : n = 5
Output : 55
= 1 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 +
4 + 5 + 5 + 5 + 5 + 5.
= 55
Input : n = 10
Output : 385
Addition method: In addition method sum all the elements one by one.
Below is the implementation of this approach.
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
sum = sum + i;
return sum;
}
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
|
Java
public class GfG{
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
sum = sum + i;
return sum;
}
public static void main(String s[])
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
|
Python3
import math
def sumOfSeries( n):
sum = 0
for i in range(1, n+1):
sum = sum + i * i
return sum
n = 10
print (sumOfSeries(n))
|
C#
using System;
public class GfG {
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
sum = sum + i;
return sum;
}
public static void Main()
{
int n = 10;
Console.Write(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries($n)
{
$sum = 0;
for ($i = 1; $i <= $n; $i++)
for ($j = 1; $j <= $i; $j++)
$sum = $sum + $i;
return $sum;
}
$n = 10;
echo(sumOfSeries($n));
?>
|
Output:
385
Time Complexity: O(n2)
Multiplication method:In multiplication method every elements multiply by itself and then add them.
Input n = 10
sum = 1 + 2 + 2 + 3 + 3 + 3 + 4 + . . . + 10
= 1 + 2 * 2 + 3 * 3 + 4 * 4 + . . . + 10 * 10
= 1 + 4 + 9 + 16 + . . . + 100
= 385
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i * i;
return sum;
}
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
|
Java
public class GfG{
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i * i;
return sum;
}
public static void main(String args[])
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
|
Python3
import math
def sumOfSeries( n):
sum = 0
for i in range(1, n+1):
sum = sum + i * i
return sum
n = 10
print (sumOfSeries(n))
|
C#
using System;
class GfG {
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i * i;
return sum;
}
public static void Main()
{
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries($n)
{
$sum = 0;
for ($i = 1; $i <= $n; $i++)
$sum = $sum + $i * $i;
return $sum;
}
$n = 10;
echo(sumOfSeries($n));
?>
|
Output:
385
Time Complexity: O(n)
Using formula: We also use formula to find the sum of series.
Input n = 10;
Sum of series = (n * (n + 1) * (2 * n + 1)) / 6
put n = 10 in the above formula
sum = (10 * (10 + 1) * (2 * 10 + 1)) / 6
= (10 * 11 * 21) / 6
= 385
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 1)) / 6;
}
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
|
Java
public class GfG
{
static int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 1)) / 6;
}
public static void main(String s[])
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
|
Python3
import math
def sumOfSeries( n):
return ((n * (n + 1) * (2 * n + 1)) / 6)
n = 10
print (sumOfSeries(n))
|
C#
using System;
public class GfG {
static int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 1)) / 6;
}
public static void Main()
{
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries($n)
{
return ($n * ($n + 1) *
(2 * $n + 1)) / 6;
}
$n = 10;
echo(sumOfSeries($n));
?>
|
Output :
385
Time Complexity: O(1)
Please refer sum of squares of natural numbers for details of above formula and more optiimizations.
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.
Recommended Posts:
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of the series 1 + x + x^2+ x^3+ .. + x^n
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find Nth term of series 0, 9, 22, 39, 60, 85, 114, 147, .....
- Program to find the Nth term of the series 0, 3/1, 8/3, 15/5........
- Program to find Nth term of series 4, 14, 28, 46, 68, 94, 124, 158, .....
- Program to find Nth term of series 0, 11, 28, 51, 79, 115, 156, 203, ....
- Program to find Nth term of series 2, 12, 28, 50, 77, 112, 152, 198, .....
- Program to find Nth term of series 7, 21, 49, 91, 147, 217, ......
- Program to find the Nth term of the series 0, 5, 18, 39, 67, 105, 150, 203, ...
- Program to find the nth term of the series -2, 4, -6, 8....
- Program to find Nth term of series 0, 10, 30, 60, 99, 150, 210, 280...........
- Program to find Nth term of series 0, 7, 18, 33, 51, 75, 102, 133, .....
- Program to find the Nth term of series -1, 2, 11, 26, 47......

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.