📜  通过抛N个骰子获得的值的乘积获得素数的可能性

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

给定一个表示骰子数量的整数N ,任务是找到出现在N个投掷骰子的顶面上的数字乘积是质数的概率。所有N个骰子必须同时投掷。

例子:

天真的方法:解决此问题的最简单方法是通过同时抛出N个骰子在N个骰子的顶面上生成所有可能的结果,并针对每个可能的结果检查顶面上的数字乘积是否为质数。如果发现为真,则增加计数器。最后,打印获得顶面上数字乘积作为质数的概率。

时间复杂度: O(6 N * N)
辅助空间: O(1)

高效的方法:为了优化上述方法,我们的想法是利用以下事实:只有(N – 1)个数字为1且其余数字为质数时, N的乘积为质数。以下是观察结果:

请按照以下步骤解决此问题:

  • 初始化一个变量,例如N_E,以存储有利结果的计数。
  • 初始化一个变量,例如N_S,以存储样本空间的数量。
  • 更新N_E = 3 * N。
  • 更新N_S = 6 N。
  • 最后,打印(N_E / N_S)的值。

下面是上述方法的实现

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
 
// Function to find the value
// of power(X, N)
long long int power(long long int x,
                    long long int N)
{
    // Stores the value
    // of (X ^ N)
    long long int res = 1;
 
    // Calculate the value of
    // power(x, N)
    while (N > 0) {
         
       // If N is odd
       if(N & 1) {
            
           //Update res
           res = (res * x);
       }
        
       //Update x
       x = (x * x);
        
       //Update N
       N = N >> 1;
        
    }
    return res;
}
 
// Function to find the probability of
// obtaining a prime number as the
// product of N thrown dices
void probablityPrimeprod(long long int N)
{
    // Stores count of favorable outcomes
    long long int N_E = 3 * N;
     
    // Stores count of sample space
    long long int N_S = power(6, N);
     
    // Print the required probablity
    cout<


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Function to find the value
// of power(X, N)
static int power(int x, int N)
{
     
    // Stores the value
    // of (X ^ N)
    int res = 1;
 
    // Calculate the value of
    // power(x, N)
    while (N > 0)
    {
         
        // If N is odd
        if (N % 2 == 1)
        {
             
            // Update res
            res = (res * x);
        }
         
        // Update x
        x = (x * x);
         
        // Update N
        N = N >> 1;
    }
    return res;
}
 
// Function to find the probability of
// obtaining a prime number as the
// product of N thrown dices
static void probablityPrimeprod(int N)
{
     
    // Stores count of favorable outcomes
    int N_E = 3 * N;
     
    // Stores count of sample space
    int N_S = power(6, N);
     
    // Print the required probablity
    System.out.print(N_E + " / " + N_S);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 2;
     
    probablityPrimeprod(N);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the value
# of power(X, N)
def power(x, N):
     
    # Stores the value
    # of (X ^ N)
    res = 1
 
    # Calculate the value of
    # power(x, N)
    while (N > 0):
 
        # If N is odd
        if (N % 2 == 1):
             
            # Update res
            res = (res * x)
 
        # Update x
        x = (x * x)
 
        # Update N
        N = N >> 1
 
    return res
 
# Function to find the probability of
# obtaining a prime number as the
# product of N thrown dices
def probablityPrimeprod(N):
     
    # Stores count of favorable outcomes
    N_E = 3 * N
 
    # Stores count of sample space
    N_S = power(6, N)
 
    # Prthe required probablity
    print(N_E, " / ", N_S)
 
# Driver code
if __name__ == '__main__':
     
    N = 2
 
    probablityPrimeprod(N)
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG{
     
// Function to find the
// value of power(X, N)
static int power(int x,
                 int N)
{   
  // Stores the value
  // of (X ^ N)
  int res = 1;
 
  // Calculate the value
  // of power(x, N)
  while (N > 0)
  {
    // If N is odd
    if (N % 2 == 1)
    {
      // Update res
      res = (res * x);
    }
 
    // Update x
    x = (x * x);
 
    // Update N
    N = N >> 1;
  }
  return res;
}
 
// Function to find the probability
// of obtaining a prime number as
// the product of N thrown dices
static void probablityPrimeprod(int N)
{   
  // Stores count of favorable
  // outcomes
  int N_E = 3 * N;
 
  // Stores count of sample
  // space
  int N_S = power(6, N);
 
  // Print the required
  // probablity
  Console.Write(N_E + " / " + N_S);
}
 
// Driver code
public static void Main(String[] args)
{
  int N = 2;
  probablityPrimeprod(N);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
6 / 36

时间复杂度: O(log 2 N)
辅助空间: O(1)