Producto de N términos de una serie geométrica dada

Dado un número N , el primer término ‘a’ y una razón común ‘r’ de una Progresión Geométrica , la tarea es encontrar el producto de los primeros N términos de la Progresión Geométrica dada.

Ejemplos: 

Entrada: a = 1, r = 2, N = 4 
Salida: 64 
Explicación: 
Los primeros cuatro términos para el GP anterior son 1, 2, 4, 8. 
El producto de cuatro números es 1*2*4*8 = 64

Entrada: a = 1, r = 0.5, N = 3 
Salida: 0.125 
Explicación: 
Los primeros tres términos para el GP anterior son 1, 1/2, 1/4. 
El producto de cuatro números es 1*(1/2)*(1/4) = 1/8 = 0,125 

Enfoque ingenuo: la idea es encontrar todos los N términos de la progresión geométrica dada y multiplicar todos los términos obtenidos.

A continuación se muestra la implementación del enfoque anterior: 

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>
Producción: 

64

 

Enfoque eficiente: El enfoque eficiente es observar que el producto de todos los N términos de la progresión geométrica dada forma una fórmula: 

El médico general es 

a, a*r, a*r^{2}, a*r^{3}, a*r^{4}, ...
Producto hasta 1er término = 
a
Producto hasta 2do término = 
a^{2}*r
Producto hasta 3er término = 
a^{3}*r^{3}
Producto hasta 4to término = 
a^{4}*r^{6}



Producto hasta N términos = 
a^N * r^{\frac {N(N - 1)}{2}
  

A continuación se muestra la implementación del enfoque anterior: 

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>
Producción: 

64

 

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Otro enfoque: 

Sea un GP de N términos como sigue:  

a, a*r, a*r^{2}, a*r^{3}, a*r^{4}, ..., a*r^{4}, a*r^{N-2}, a*r^{N-1}
Producto del 1er y último término = 
(a)*(a*r^{N-1})
 = 
(a^{2}*r^{N-1})
Producto del 2do y 2do último término = 
(a*r)*(a*r^{N-2})
 = 
(a^{2}*r^{N-1})
Producto del 3er y 3er último término = 
(a*r^{2})*(a*r^{N-3})
 = 
(a^{2}*r^{N-1})
y así sucesivamente… 
En una progresión geométrica, el producto del primer y último término es igual al producto del segundo y penúltimo término y así sucesivamente, sin importar cuántos términos estemos considerando en una progresión geométrica.
Por lo tanto, si 
A_{1}         A_{N}          es el primer y el último término de un GP, ​​entonces el producto de los primeros N términos del GP será 
P_n = \sqrt{(a_1 \cdot a_n)^n}
 

A continuación se muestra la implementación del enfoque anterior: 

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>
Producción: 

64

 

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *