📌  相关文章
📜  给定几何级数的N个项的乘积

📅  最后修改于: 2021-04-24 05:31:25             🧑  作者: Mango

给定数字N ,几何级数的第一项‘a’和公共比率‘r’ ,任务是找到给定几何级数的前N个项的乘积。

例子:

天真的方法:这个想法是找到给定几何级数的所有N个项,并将获得的所有项相乘。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


C++
// C++ program for the above approach
#include 
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


C++
// C++ program for the above approach
#include 
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


输出:
64

高效方法:高效方法是观察给定几何级数的所有N个项的乘积形成一个公式:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
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

Java脚本


输出:
64

另一种方法:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
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

Java脚本


输出:
64