Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n

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++



filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find
// sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
#include <bits/stdc++.h>
using namespace std;
  
// Function that find
// sum of series.
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;
}
  
// Driver function
int main()
{
    int n = 10;
  
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to
// find sum of 
// series
// 1 + 2 + 2 + 3 + 
// . . . + n
public class GfG{
  
    // Function that find 
    // sum of series.
    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;
    }
      
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
          
    }
  
// This code is contributed by Gitanjali

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to 
# find sum of series
# 1 + 2 + 2 + 3 + 
# . . . + n
import math 
  
# Function that find
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
  
# Driver method
n = 10
  
# Function call
print (sumOfSeries(n))
  
# This code is contributed by Gitanjali 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find sum of
// series 1 + 2 + 2 + 3 + . . . + n
using System;
  
public class GfG {
  
    // Function that find
    // sum of series.
    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;
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.Write(sumOfSeries(n));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to find
// sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
  
// Function that find
// sum of series.
function sumOfSeries($n)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
        for ($j = 1; $j <= $i; $j++)
            $sum = $sum + $i;
    return $sum;
}
  
// Driver Code
$n = 10;
  
// Function call
echo(sumOfSeries($n));
  
// This code is contributed by Ajit.
?>

chevron_right



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find
// sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
#include <bits/stdc++.h>
using namespace std;
  
// Function to find 
// sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + i * i;
    return sum;
}
  
// Driver function.
int main()
{
    int n = 10;
  
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to 
// find sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
public class GfG{
  
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
  
    // Driver Code 
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
          
    }
  
// This code is contributed by Gitanjali

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to 
# find sum of series
# 1 + 2 + 2 + 3 + 
# . . . + n
import math 
  
# Function that find 
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
  
# Driver method
n = 10
# Function call
print (sumOfSeries(n))
  
# This code is contributed by Gitanjali.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
  
class GfG {
  
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
  
    // Driver Code 
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
          
    }
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to find
// sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
  
// Function to find 
// sum of series.
function sumOfSeries($n)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
        $sum = $sum + $i * $i;
    return $sum;
}
  
// Driver Code
$n = 10;
  
// Function call
echo(sumOfSeries($n));
  
// This code is contributed by Ajit.
?>

chevron_right



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to
// find sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
#include <bits/stdc++.h>
using namespace std;
  
// Function to find 
// sum of series.
int sumOfSeries(int n)
{
    return (n * (n + 1) * (2 * n + 1)) / 6;
}
  
// Driver function
int main()
{
    int n = 10;
  
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to 
// find sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
public class GfG
{
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
      
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
          
    }
  
// This code is contributed by 'Gitanjali'.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 + 
# . . . + n
import math 
  
# Function that find
# sum of series.
def sumOfSeries( n):
    return ((n * (n + 1) * (2 * n + 1)) / 6)
  
# Driver method
n = 10
  
# Function call
print (sumOfSeries(n))
  
# This code is contributed by Gitanjali

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
  
public class GfG {
      
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
    }
}
  
// This code is contributed by 'vt_m'.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to
// find sum of series
// 1 + 2 + 2 + 3 + 
// . . . + n
  
// Function to find 
// sum of series.
function sumOfSeries($n)
{
    return ($n * ($n + 1) * 
           (2 * $n + 1)) / 6;
}
  
// Driver Code
$n = 10;
  
// Function call
echo(sumOfSeries($n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output :

385

Time Complexity: O(1)

Please refer sum of squares of natural numbers for details of above formula and more optiimizations.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : jit_t, vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.