Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion

06 Jul, 2022
  Example : 
Input : 2
Output: 7
Series: (1) + (2*3)

Input : 4
Output: 5167
Series: (1) + (2*3) + (4*5*6) + (7*8*9*10) 
 
 
C++
C
Java
Python3
C#
PHP
Javascript
// CPP Program to print the solution// of the series f(n)= (1) + (2*3)// + (4*5*6) ... n using recursion#include<bits/stdc++.h>using namespace std; // Recursive function for// finding sum of series// calculated - number of terms till//             which sum of terms has//             been calculated// current - number of terms for which//             sum has to becalculated// N         - Number of terms in the//             function to be calculatedint seriesSum(int calculated, int current,                            int N){    int i, cur = 1;     // checking termination condition    if (current == N + 1)        return 0;     // product of terms till current    for (i = calculated; i < calculated +                            current; i++)        cur *= i;     // recursive call for adding    // terms next in the series    return cur + seriesSum(i, current + 1, N);} // Driver Codeint main(){    // input number of terms in the series    int N = 5;     // invoking the function to    // calculate the sum    cout<<seriesSum(1, 1, N)<<endl;     return 0;}
// C Program to print the solution// of the series f(n)= (1) + (2*3)// + (4*5*6) ... n using recursion#include <stdio.h> // Recursive function for// finding sum of series// calculated - number of terms till//                which sum of terms has//              been calculated// current - number of terms for which//             sum has to becalculated// N         - Number of terms in the//             function to be calculatedint seriesSum(int calculated, int current,                              int N){    int i, cur = 1;     // checking termination condition    if (current == N + 1)        return 0;     // product of terms till current    for (i = calculated; i < calculated +                            current; i++)        cur *= i;     // recursive call for adding    // terms next in the series    return cur + seriesSum(i, current + 1, N);} // Driver Codeint main(){    // input number of terms in the series    int N = 5;     // invoking the function to    // calculate the sum    printf("%d\n", seriesSum(1, 1, N));     return 0;}
// Java Program to print the// solution of the series// f(n)= (1) + (2*3) + (4*5*6)// ... n using recursion class GFG{         /**    * Recursive method for finding    * sum of series    *    * @param calculated number of terms    * till which sum of terms has been    * calculated @param current number of    * terms for which sum has to be calculated.    * @param N Number of terms in the function    * to be calculated @return sum    */         static int seriesSum(int calculated,                         int current,                         int N)    {        int i, cur = 1;             // checking termination condition        if (current == N + 1)            return 0;             // product of terms till current        for (i = calculated; i < calculated +                                current; i++)            cur *= i;             // recursive call for adding        // terms next in the series        return cur + seriesSum(i, current + 1, N);    }         // Driver Code    public static void main(String[] args)    {        // input number of        // terms in the series        int N = 5;             // invoking the method        // to calculate the sum        System.out.println(seriesSum(1, 1, N));    }}
# Python3 Program to print the solution# of the series f(n)= (1) + (2*3) + (4*5*6)# ... n using recursion # Recursive function for finding sum of series# calculated - number of terms till#               which sum of terms#               has been calculated# current - number of terms for#            which sum has to be#            calculated# N     - Number of terms in the#       function to be calculateddef seriesSum(calculated, current, N):     i = calculated;    cur = 1;     # checking termination condition    if (current == N + 1):        return 0;     # product of terms till current    while (i < calculated + current):        cur *= i;        i += 1;     # recursive call for adding    # terms next in the series    return cur + seriesSum(i, current + 1, N); # Driver code # input number of terms in the seriesN = 5; # invoking the function# to calculate the sumprint(seriesSum(1, 1, N)); # This code is contributed by mits
// C# Program to print the// solution of the series// f(n)= (1) + (2*3) + (4*5*6)// ... n using recursionusing System; class GFG{         // Recursive function for    // finding sum of series    // calculated - number of terms till     //                which sum of terms    //              has been calculated    // current    - number of terms for which    //                sum has to be calculated    // N         - Number of terms in the     //               function to be calculated    static int seriesSum(int calculated,                         int current,                         int N)    {                 int i, cur = 1;             // checking termination condition        if (current == N + 1)            return 0;             // product of terms till current        for (i = calculated; i < calculated +                                current; i++)            cur *= i;             // recursive call for adding terms        // next in the series        return cur + seriesSum(i, current + 1, N);    }         // Driver Code    public static void Main()    {                 // input number of terms        // in the series        int N = 5;             // invoking the method to        // calculate the sum        Console.WriteLine(seriesSum(1, 1, N));    }} // This code is contributed by vt_m.
<?php// PHP Program to print the// solution of the series// f(n)= (1) + (2*3) + (4*5*6)// ... n using recursion // Recursive function for// finding sum of series// calculated - number of terms till//                which sum of terms//              has been calculated// current -    number of terms for//                which sum has to be//              calculated// N         - Number of terms in the//             function to be calculatedfunction seriesSum($calculated, $current, $N){    $i; $cur = 1;     // checking termination condition    if ($current == $N + 1)        return 0;     // product of terms till current    for ($i = $calculated; $i < $calculated +                              $current; $i++)        $cur *= $i;     // recursive call for adding    // terms next in the series    return $cur + seriesSum($i, $current + 1, $N);} // Driver code // input number of// terms in the series$N = 5; // invoking the function// to calculate the sumecho(seriesSum(1, 1, $N)); // This code is contributed by Ajit.?>
<script> // JavaScript Program to print the// solution of the series// f(n)= (1) + (2*3) + (4*5*6)// ... n using recursion /**    * Recursive method for finding    * sum of series    *    * @param calculated number of terms    * till which sum of terms has been    * calculated @param current number of    * terms for which sum has to be calculated.    * @param N Number of terms in the function    * to be calculated @return sum    */           function seriesSum(calculated,                         current, N)    {        let i, cur = 1;               // checking termination condition        if (current == N + 1)            return 0;               // product of terms till current        for (i = calculated; i < calculated +                                current; i++)            cur *= i;               // recursive call for adding        // terms next in the series        return cur + seriesSum(i, current + 1, N);    }        // Driver Code         // input number of        // terms in the series        let N = 5;               // invoking the method        // to calculate the sum        document.write(seriesSum(1, 1, N));         // This code is contributed by target_2.</script>
Output :  
365527 
Time Complexity: O(n2)
Auxiliary Space: O(n)Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
jit_t
SURENDRA_GANGWAR
Mithun Kumar
target_2
akashish__
MAQ Software
series
C Language
C++
Mathematical
School Programming
MAQ Software
Mathematical
series
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Initialize a vector in C++ (7 different ways)
Templates in C++ with Examples
Operator Overloading in C++
Inheritance in C++