4 2 5 1 1 3 2 3 4

4 2 5 1 1 3 2 3 4

Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + …… + (1+2+3+4+…+n)

View Discussion

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like Article

    Given the value of
    n, we need to find the sum of the series where i-th term is sum of first i natural numbers.
    Examples :

    Input  : n = 5    Output : 35 Explanation : (1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) = 35  Input  : n = 10 Output : 220 Explanation : (1) + (1+2) + (1+2+3) +  .... +(1+2+3+4+.....+10) = 220

    Naive Approach :
    Below is implementation of above series :

    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 += j;


    return
    sum;

    }

    int
    main()

    {


    int
    n = 10;


    cout << sumOfSeries(n);


    return
    0;

    }

    Java

    import
    java.util.*;

    class
    GFG {


    static
    int
    sumOfSeries(
    int
    n)


    {


    int
    sum =


    ;


    for
    (
    int
    i =

    1
    ; i <= n ; i++)


    for
    (
    int
    j =

    1
    ; j <= i ; j++)


    sum += j;


    return
    sum;


    }


    public
    static
    void
    main(String[] args)


    {


    int
    n =

    10
    ;


    System.out.println(sumOfSeries(n));


    }

    }

    Python

    def
    sumOfSeries(n):


    return
    sum
    ([i
    *
    (i
    +
    1
    )
    /
    2
    for
    i

    in
    range
    (
    1
    , n

    +
    1
    )])

    if
    __name__

    =
    =
    "__main__"
    :


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


    for
    (
    int
    j = 1; j <= i; j++)


    sum += j;


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

    $j
    ;


    return
    $sum
    ;

    }

    $n
    = 10;

    echo
    (sumOfSeries(
    $n
    ));

    ?>

    Javascript

    <script>


    function
    sumOfSeries(n)


    {


    let sum = 0;


    for
    (let i = 1 ; i <= n ; i++)


    for
    (let j = 1 ; j <= i ; j++)


    sum += j;


    return
    sum;


    }


    let n = 10;


    document.write(sumOfSeries(n));

    </script>

    Output :

    220

    Efficient Approach :

    Letterm of the series 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4)…(1 + 2 + 3 +..n) be denoted as
    an

              an
              
              = Σn
              1
              i
              =
              \frac{n (n + 1)}{2}
              =
              \frac{(n^2 + n)}{2}
              Sum of n-terms of series  Σn
              1
              an
              = Σn
              1
              \frac{(n^2 + n)}{2}
              =
              \frac{1}{2}
              Σ
              [
              n^2
              ]
              + Σ
              [
              n
              ]
              =
              \frac{1}{2}
              *
              \frac{n(n + 1)(2n + 1)}{6}
              +
              \frac{1}{2}
              *
              \frac{n(n+1)}{2}
              =
              \frac{n(n+1)(2n+4)}{12}
            

    Below is implementation of above approach :

    Baca :   Pertanyaan Tentang Perempuan Di Era Milenial

    C++

    #include <bits/stdc++.h>

    using
    namespace
    std;

    int
    sumOfSeries(
    int
    n)

    {


    return
    (n * (n + 1) * (2 * n + 4)) / 12;

    }

    int
    main()

    {


    int
    n = 10;


    cout << sumOfSeries(n);

    }

    Java

    import
    java.util.*;

    class
    GFG {


    static
    int
    sumOfSeries(
    int
    n)


    {


    return
    (n * (n +

    1
    ) *


    (
    2
    * n +

    4
    )) /

    12
    ;


    }


    public
    static
    void
    main(String[] args)


    {


    int
    n =

    10
    ;


    System.out.println(sumOfSeries(n));


    }

    }

    Python

    def
    sumOfSeries(n):


    return
    (n

    *
    (n

    +
    1
    )

    *
    (
    2
    *
    n

    +
    4
    ))

    /
    12
    ;

    if
    __name__

    =
    =
    '__main__'
    :


    n

    =
    10


    print
    (sumOfSeries(n))

    C#

    using
    System;

    class
    GFG {


    static
    int
    sumOfSeries(
    int
    n)


    {


    return
    (n * (n + 1) * (2 * n + 4)) / 12;


    }


    public
    static
    void
    Main()


    {


    int
    n = 10;


    Console.Write(sumOfSeries(n));


    }

    }

    PHP

    <?php

    function
    sumOfSeries(
    $n
    )

    {


    return
    (
    $n
    * (
    $n
    + 1) *


    (2 *

    $n
    + 4)) / 12;

    }

    $n
    = 10;

    echo
    (sumOfSeries(
    $n
    ));

    ?>

    Javascript

    <script>


    function
    sumOfSeries(n)


    {


    return
    (n * (n + 1) *


    (2 * n + 4)) / 12;


    }


    let n = 10;


    document.write(sumOfSeries(n));

    </script>

    Output :

    220

    Time Complexity:
    O(1)
    Auxiliary Space:
    O(1)

    4 2 5 1 1 3 2 3 4

    Sumber: https://www.geeksforgeeks.org/sum-of-the-series-1-12-123-1234-1234-n/

    Check Also

    Harga Beras 10 Kg Di Pasar

    Harga Beras 10 Kg Di Pasar 4 menit Kamu pasti sudah sering sekali mendengar ungkapan, …