The Wayback Machine - https://web.archive.org/web/20240717181823/https://www.geeksforgeeks.org/find-geometric-sum-of-the-series-using-recursion/
Open In App

Find geometric sum of the series using recursion

Last Updated : 09 Aug, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Given an integer N, we need to find the geometric sum of the following series using recursion. 
 

1 + 1/3 + 1/9 + 1/27 + … + 1/(3^n) 
 

Examples: 
 

Input N = 5 
Output: 1.49794

Input: N = 7
Output: 1.49977

 

Approach:
In the above-mentioned problem, we are asked to use recursion. We will calculate the last term and call recursion on the remaining n-1 terms each time. The final sum returned is the result.
Below is the implementation of the above approach: 
 

C++




// CPP implementation to Find the
// geometric sum of the series using recursion
 
#include <bits/stdc++.h>
using namespace std;
 
// function to find the sum of given series
double sum(int n)
{
    // base case
    if (n == 0)
        return 1;
 
    // calculate the sum each time
    double ans = 1 / (double)pow(3, n) + sum(n - 1);
 
    // return final answer
    return ans;
}
 
// Driver code
int main()
{
 
    // integer initialisation
    int n = 5;
 
    cout << sum(n) << endl;
 
    return 0;
}


Java




// JAVA implementation to Find the
// geometric sum of the series using recursion
 
import java.util.*;
 
class GFG {
 
    static double sum(int n)
    {
        // base case
        if (n == 0)
            return 1;
 
        // calculate the sum each time
        double ans = 1 / (double)Math.pow(3, n) + sum(n - 1);
 
        // return final answer
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // integer initialisation
        int n = 5;
 
        // print result
        System.out.println(sum(n));
    }
}


Python3




# CPP implementation to Find the
# geometric sum of the series using recursion
 
 
def sum(n):
     
    # base case
    if n == 0:
        return 1
     
    # calculate the sum each time
    # and return final answer
    return 1 / pow(3, n) + sum(n-1)
 
n = 5;
 
print(sum(n));


C#




// C# implementation to Find the
// geometric sum of the series using recursion
 
using System;
 
class GFG {
 
    static double sum(int n)
    {
        // base case
        if (n == 0)
            return 1;
 
        // calculate the sum each time
        double ans = 1 / (double)Math.Pow(3, n) + sum(n - 1);
 
        // return final answer
        return ans;
    }
 
    // Driver code
    static public void Main()
    {
        int n = 5;
 
        Console.WriteLine(sum(n));
    }
}


Javascript




<script>
 
// Javascript implementation to Find the
// geometric sum of the series using recursion
 
// function to find the sum of given series
function sum(n)
{
    // base case
    if (n == 0)
        return 1;
 
    // calculate the sum each time
    var ans = 1 / Math.pow(3, n) + sum(n - 1);
 
    // return final answer
    return ans;
}
 
// Driver code
// integer initialisation
var n = 5;
document.write( sum(n).toFixed(5));
 
</script>


Output: 

1.49794

 

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



Previous Article
Next Article

Similar Reads

Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and the common ratio is denoted by r. The series looks like this:- [Tex]a, ar, ar^2, ar^3, ar^4,... [/Tex]The task is to find the sum of such a series, mod M. Examples: Input: a = 1, r = 2, N = 10000, M = 10000 Output: 8751 In
7 min read
Program for sum of geometric series
A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and common ratio is denoted by r. The series looks like this :- a, ar, ar2, ar3, ar4, . . .. The task is to find the sum of such a series. Examples : Input : a = 1 r = 0.5 n = 10Output : 1.99805Input : a = 2 r = 2 n = 15Output
7 min read
Program to find Nth term of given Geometric Progression (GP) series
Given the first term a, common ratio r, and an integer N of a Geometric Progression (GP) series, the task is to find the N-th term of the series. Examples: Input: a=2, r=2, N=4 Output: The 4th term of the series is: 16 Explanation: The series is 2, 4, 8, 16, ... The 4th term is 16. Input: 2a=2, r=3, N=5 Output: The 5th term of the series is: 162 Ex
7 min read
Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
Given an integer n, the task is to find the sum of the series 11 + 22 + 33 + ..... + nn using recursion. Examples: Input: n = 2 Output: 5 11 + 22 = 1 + 4 = 5 Input: n = 3 Output: 32 11 + 22 + 33 = 1 + 4 + 27 = 32 Approach: Starting from n, start adding all the terms of the series one by one with the value of n getting decremented by 1 in each recur
3 min read
Product of N terms of a given Geometric series
Given a number N, the first term 'a' and a common ratio 'r' of a Geometric Progression, the task is to find the product of first N terms of the given Geometric Progression. Examples: Input: a = 1, r = 2, N = 4 Output: 64 Explanation: First four term for the above G.P. is 1, 2, 4, 8. Product of four numbers is 1*2*4*8 = 64 Input: a = 1, r = 0.5, N =
11 min read
Number of terms in Geometric Series with given conditions
A geometric progression is a sequence of integers b1, b2, b3, ..., where for each i &gt; 1, the respective term satisfies the condition bi = bi-1 * q, where q is called the common ratio of the progression.Given geometric progression b defined by two integers b1 and q, and m "bad" integers a1, a2, .., am, and an integer l, write all progression term
12 min read
Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Print Fibonacci Series in reverse order using Recursion
Given an integer N, the task is to print the first N terms of the Fibonacci series in reverse order using Recursion. Examples: Input: N = 5Output: 3 2 1 1 0Explanation: First five terms are - 0 1 1 2 3. Input: N = 10Output: 34 21 13 8 5 3 2 1 1 0 Approach: The idea is to use recursion in a way that keeps calling the same function again till N is gr
3 min read
Program to Calculate e^x by Recursion ( using Taylor Series )
The value of the Exponential function can be calculated using Taylor Series. [Tex]e^x[/Tex] = 1 + x/1! + [Tex]x^2[/Tex]/2! + [Tex]x^3[/Tex]/3! + ...... + until n termsAs the number of terms increases the more precise value of ex is obtained. To find e^x using the recursive function, we need to use static variables. A function can return only one va
5 min read
Find Harmonic mean using Arithmetic mean and Geometric mean
Given two numbers, first calculate arithmetic mean and geometric mean of these two numbers. Using the arithmetic mean and geometric mean so calculated, find the harmonic mean between the two numbers. Examples: Input : a = 2 b = 4 Output : 2.666 Input : a = 5 b = 15 Output : 7.500 Arithmetic Mean: Arithmetic Mean 'AM' between two numbers a and b is
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg