Skip to content
Related Articles

Related Articles

Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
  • Difficulty Level : Easy
  • Last Updated : 27 Apr, 2021

Given n, we need to find sum of 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)! 
Examples: 
 

Input: 1 
Output: 2
Input: 3 
Output: 242 
 

We may assume that overflow does not happen.
 

A simple solution is to compute terms one by one and add to result.
An efficient solution is based on direct formula 2 + (n*n + n – 2) * (n + 1)! 
The working of formula is based on this post. 
 

C++




// CPP program to find sum of the series.
#include <bits/stdc++.h>
using namespace std;
 
int factorial(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
 
// Function to calculate required series
int calculateSeries(int n)
{
    return 2 + (n * n + n - 2) * factorial(n + 1);
}
 
// Drivers code
int main()
{
    int n = 3;
    cout << calculateSeries(n);
    return 0;
}

Java




// java program to find sum of the series.
import java.io.*;
 
class GFG {
     
    static int factorial(int n)
    {
        int res = 1;
        for (int i = 2; i <= n; i++)
            res = res * i;
        return res;
    }
     
    // Function to calculate required series
    static int calculateSeries(int n)
    {
        return 2 + (n * n + n - 2)
                      * factorial(n + 1);
    }
     
    // Drivers code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println(calculateSeries(n));
    }
}
 
// This code is contributed by anuj_67.

Python3




# Python program to find sum of
# the series.
import math
 
def factorial(n):
    res = 1
    i = 2
    for i in (n+1):
        res = res * i
    return res
     
# Function to calculate required
# series
def calculateSeries(n):
    return (2 + (n * n + n - 2)
        * math.factorial(n + 1))
 
# Driver code
n = 3
print(calculateSeries(n))
 
# This code is contributed by
# Prateek bajaj

C#




// C# program to find sum of the series.
using System;
class GFG {
     
    static int factorial(int n)
    {
        int res = 1;
        for (int i = 2; i <= n; i++)
            res = res * i;
        return res;
    }
     
    // Function to calculate required series
    static int calculateSeries(int n)
    {
        return 2 + (n * n + n - 2)
                 * factorial(n + 1);
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 3;
        Console.WriteLine(calculateSeries(n));
    }
}
 
// This code is contributed by anuj_67.

PHP




<?php
// PHP program to find sum of the series.
 
function factorial( $n)
{
    $res = 1;
    for ( $i = 2; $i <= $n; $i++)
        $res = $res * $i;
    return $res;
}
 
// Function to calculate required series
function calculateSeries( $n)
{
    return 2 + ($n * $n + $n - 2) *
                 factorial($n + 1);
}
 
    // Driver code
    $n = 3;
    echo calculateSeries($n);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
// java script  program to find sum of the series.
function factorial( n)
    {
        let res = 1;
        for (let i = 2; i <= n; i++)
            res = res * i;
        return res;
    }
     
    // Function to calculate required series
    function calculateSeries( n)
    {
        return 2 + (n * n + n - 2)
                      * factorial(n + 1);
    }
     
    // Drivers code
        let n = 3;
        document.write(calculateSeries(n));
         
// This code is contributed by mohan pavan
</script>
Output: 
242

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :