Open In App

Product of N terms of a given Geometric series

Improve
Improve
Like Article
Like
Save
Share
Report

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 = 3 
Output: 0.125 
Explanation: 
First three term for the above G.P. is 1, 1/2, 1/4. 
Product of four numbers is 1*(1/2)*(1/4) = 1/8 = 0.125 

Naive Approach: The idea is to find all the N terms of the given geometric progression and multiply all the terms obtained.

Below is the implementation of the above approach: 

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate product of
// geometric series
float productOfGP(float a, float r, int n)
{
    // Initialise final product with 1
    float product = 1;
 
    for (int i = 0; i < n; i++) {
        // Multiply product with each
        // term stored in a
        product = product * a;
        a = a * r;
    }
 
    // Return the final product
    return product;
}
 
// Driver Code
int main()
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    cout << productOfGP(a, r, N);
}

                    

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to calculate product of
// geometric series
static float productOfGP(float a,
                         float r, int n)
{
    // Initialise final product with 1
    float product = 1;
 
    for (int i = 0; i < n; i++)
    {
        // Multiply product with each
        // term stored in a
        product = product * a;
        a = a * r;
    }
 
    // Return the final product
    return product;
}
 
// Driver Code
public static void main(String args[])
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    System.out.print(productOfGP(a, r, N));
}
}
 
// This code is contributed by Code_Mech

                    

Python3

# Python3 program for the above approach
 
# Function to calculate product of
# geometric series
def productOfGP(a, r, n):
     
    # Initialise final product with 1
    product = 1;
 
    for i in range(0, n):
         
        # Multiply product with each
        # term stored in a
        product = product * a;
        a = a * r;
 
    # Return the final product
    return product;
     
# Driver code
 
# Given first term
# and common ratio
a = 1
r = 2;
 
# Number of terms
N = 4;
 
# Function Call
print(productOfGP(a, r, N))
 
# This code is contributed by Pratima Pandey.

                    

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to calculate product of
// geometric series
static float productOfGP(float a,
                         float r, int n)
{
    // Initialise final product with 1
    float product = 1;
 
    for (int i = 0; i < n; i++)
    {
        // Multiply product with each
        // term stored in a
        product = product * a;
        a = a * r;
    }
 
    // Return the final product
    return product;
}
 
// Driver Code
public static void Main()
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    Console.Write(productOfGP(a, r, N));
}
}
 
// This code is contributed by Code_Mech

                    

Javascript

<script>
    // Javascript program for the above approach 
 
    // Function to calculate product of
    // geometric series
    function productOfGP(a, r, n)
    {
     
        // Initialise final product with 1
        let product = 1;
 
        for (let i = 0; i < n; i++)
        {
         
            // Multiply product with each
            // term stored in a
            product = product * a;
            a = a * r;
        }
 
        // Return the final product
        return product;
    }
     
    // Given first term
    // and common ratio
    let a = 1, r = 2;
   
    // Number of terms
    let N = 4;
   
    // Function Call
    document.write(productOfGP(a, r, N));
    
   // This code is contributed by divyesh072019.
</script>

                    

Output: 
64

 

Time Complexity: O(N), where N is the given integer.
Auxiliary Space: O(1), as constant extra space is required.

Efficient Approach: The efficient approach is to observe that the product of all the N terms of the given geometric progression forms a formula: 

The general G.P. is 

a, a*r, a*r^{2}, a*r^{3}, a*r^{4}, ...
Product upto 1st term = 
a
Product upto 2nd term = 
a^{2}*r
Product upto 3rd term = 
a^{3}*r^{3}
Product upto 4th term = 
a^{4}*r^{6}



Product upto N terms = 
a^N * r^{\frac {N(N - 1)}{2}
  

Below is the implementation of the above approach: 

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate product of
// geometric series
float productOfGP(float a, float r,
                  int n)
{
    // Return the final product with the
    // above formula
    return pow(a, n)
           * pow(r, n * (n - 1) / 2);
}
 
// Driver Code
int main()
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    cout << productOfGP(a, r, N);
}

                    

Java

// Java program for the above approach
class GFG{
     
// Function to calculate product of
// geometric series
static float productOfGP(float a, float r, int n)
{
     
    // Return the final product with the
    // above formula
    return (float)Math.pow(a, n) *
           (float)Math.pow(r, n * (n - 1) / 2);
}
 
// Driver Code
public static void main(String s[])
{
     
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    System.out.println(productOfGP(a, r, N));
}
}
 
// This code is contributed by rutvik_56

                    

Python3

# Python3 program for the above approach
 
# Function to calculate product of
# geometric series
def productOfGP(a, r, n):
 
    # Return the final product with the
    # above formula
    return pow(a, n) * pow(r, n * (n - 1) // 2);
 
# Driver Code
 
# Given first term
# and common ratio
a = 1; r = 2;
 
# Number of terms
N = 4;
 
# Function Call
print(productOfGP(a, r, N));
 
# This code is contributed by Code_Mech

                    

C#

// C# program for the above approach
using System;
class GFG{
     
// Function to calculate product of
// geometric series
static float productOfGP(float a, float r, int n)
{
     
    // Return the final product with the
    // above formula
    return (float)Math.Pow(a, n) *
           (float)Math.Pow(r, n * (n - 1) / 2);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    Console.WriteLine(productOfGP(a, r, N));
}
}
 
// This code is contributed by shivanisinghss2110

                    

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to calculate product of
// geometric series
function productOfGP(a, r, n)
{
     
    // Return the final product with the
    // above formula
    return Math.pow(a, n) *
           Math.pow(r, n * (n - 1) / 2);
}
 
// Driver code
 
// Given first term
// and common ratio
let a = 1, r = 2;
 
// Number of terms
let N = 4;
 
// Function Call
document.write(productOfGP(a, r, N));
 
// This code is contributed by mukesh07
     
</script>

                    

Output: 
64

 

Time Complexity: O(logN), for using pow function.
Auxiliary space: O(1), as constant extra space is required.

Another Approach: 

Let there be a GP of N terms as followed:  

a, a*r, a*r^{2}, a*r^{3}, a*r^{4}, ..., a*r^{4}, a*r^{N-2}, a*r^{N-1}
Product of 1st and last term = 
(a)*(a*r^{N-1})
 = 
(a^{2}*r^{N-1})
Product of 2nd and 2nd last term = 
(a*r)*(a*r^{N-2})
 = 
(a^{2}*r^{N-1})
Product of 3rd and 3rd last term = 
(a*r^{2})*(a*r^{N-3})
 = 
(a^{2}*r^{N-1})
and so on… 
In a geometric progression, the product of the first and the last term is the same as the product of the second and second last term and so on, no matter how many terms we are considering in a geometric progression.
Therefore, if 
A_{1}            and A_{N}             is the first and last terms of a GP, then the product of first N terms of the GP will be 
P_n = \sqrt{(a_1 \cdot a_n)^n}
 

Below is the implementation of the above approach: 

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate product of N
// terms of geometric series
float productOfGP(float a, float r,
                  int n)
{
    // Find the product of first
    // and the last term
    int an = a * pow(r, n - 1);
 
    // Return the sqrt of the above
    // expression to find the product
    return sqrt(pow(a * an, n));
}
 
// Driver Code
int main()
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    cout << productOfGP(a, r, N);
}

                    

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to calculate product of N
// terms of geometric series
static float productOfGP(float a, float r, int n)
{
    // Find the product of first
    // and the last term
    int an = (int)(a * (int)(Math.pow(r, n - 1)));
 
    // Return the sqrt of the above
    // expression to find the product
    return (int)Math.sqrt((int)Math.pow(a * an, n));
}
 
// Driver Code
public static void main(String args[])
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    System.out.print(productOfGP(a, r, N));
}
}
 
// This code is contributed by Code_Mech

                    

Python3

# Python3 program for the above approach
import math
 
# Function to calculate product of N
# terms of geometric series
def productOfGP(a, r, n):
     
    # Find the product of first
    # and the last term
    an = a * pow(r, n - 1);
 
    # Return the sqrt of the above
    # expression to find the product
    return (math.sqrt(pow(a * an, n)))
     
# Driver code
 
# Given first term
# and common ratio
a = 1
r = 2;
 
# Number of terms
N = 4;
 
# Function Call
print(productOfGP(a, r, N))
 
# This code is contributed by Pratima Pandey.

                    

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to calculate product of N
// terms of geometric series
static float productOfGP(float a, float r, int n)
{
    // Find the product of first
    // and the last term
    int an = (int)(a * (int)(Math.Pow(r, n - 1)));
 
    // Return the sqrt of the above
    // expression to find the product
    return (int)Math.Sqrt((int)Math.Pow(a * an, n));
}
 
// Driver Code
public static void Main()
{
    // Given first term
    // and common ratio
    float a = 1, r = 2;
 
    // Number of terms
    int N = 4;
 
    // Function Call
    Console.Write(productOfGP(a, r, N));
}
}
 
// This code is contributed by Code_Mech

                    

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to calculate product of N
// terms of geometric series
function productOfGP(a, r, n)
{
     
    // Find the product of first
    // and the last term
    let an = a * Math.pow(r, n - 1);
 
    // Return the sqrt of the above
    // expression to find the product
    return Math.sqrt(Math.pow(a * an, n));
}
 
// Driver code
 
// Given first term
// and common ratio
let a = 1, r = 2;
 
// Number of terms
let N = 4;
 
// Function Call
document.write(productOfGP(a, r, N));
 
// This code is contributed by divyeshrabadiya07
 
</script>

                    

Output: 
64

 

Time Complexity: O(logN), for using pow and sqrt functions.
Auxiliary space: O(1), as constant extra space is required.



Last Updated : 23 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads