Given an integer **N** denoting the number of dices, the task is to find the probability of the product of numbers appearing on the top faces of **N** thrown dices being a prime number. All **N** dices must be thrown simultaneously.

**Examples:**

Input: N = 2Output:6 / 36Explanation:

On throwing N(=2) dices simultaneously, the possible outcomes on the top faces of N(=2) dices having product equal to a prime number are: {(1, 2), (1, 3), (1, 5), (2, 1), (3, 1), (5, 1)}.

Therefore, the count of favourable outcomes = 6 and the count of the sample space is = 36

Therefore, the required output is (6 / 36)

Input:N = 3Output:9 / 216

**Naive Approach:** The simplest approach to solve this problem is to generate all possible outcomes on the top faces of **N** dices by throwing **N** dices simultaneously and for each possible outcome check if the product of numbers on the top faces is a prime number or not. If found to be true then increment the counter. Finally, print the probability of getting the product of numbers on the top faces as a prime number.

**Time Complexity:** O(6^{N} * N)**Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach the idea is to use the fact that the product of **N** number is a prime number only if **(N – 1)** numbers are **1** and a remaining number is a prime number. Following are the observations:

If the product of N numbers is a prime number then the value of (N – 1) numbers must be 1 and the remaining number must be a prime number.

Total count of prime numbers in the range [1, 6] is 3.

Therefore, the total number of outcomes in which the product of N numbers on the top faces as a prime number = 3 * N.

P(E) = N(E) / N(S)

P(E) = probability of getting the product of numbers on the top faces of N dices as a prime number.

N(E) = total count of favourable outcomes = 3 * N

N(S) = total number of events in the sample space = 6^{N}

Follow the steps below to solve this problem:

- Initialize a variable, say
**N_E**to store the count of favorable outcomes. - Initialize a variable, say
**N_S**to store the count of sample space. - Update
**N_E = 3 * N**. - Update
**N_S = 6**.^{N} - Finally, print the value of
**(N_E / N_S)**.

Below is the implementation of the above approach

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `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<<N_E<<` `" / "` `<<N_S;` ` ` `}` `// Driver code` `int` `main()` `{` ` ` `long` `long` `int` `N = 2;` ` ` `probablityPrimeprod(N);` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

6 / 36

**Time Complexity:** O(log_{2}N)**Auxiliary Space:** O( 1 )

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Probability of getting all possible values on throwing N dices
- Probability of getting a sum on throwing 2 Dices N times
- Maximum number of dots after throwing a dice N times
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Conditional Probability and Independence - Probability | Class 12 Maths
- Number of ways to get a given sum with n number of m-faced dices
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of prime numbers without odd prime digits
- Permutation of first N positive integers such that prime numbers are at prime indices

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.