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 = 2
Output: 6 / 36
Explanation:
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 = 3
Output: 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(6N * 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 = 6N
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 = 6N.
- Finally, print the value of (N_E / N_S).
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
long long int power( long long int x,
long long int N)
{
long long int res = 1;
while (N > 0) {
if (N & 1) {
res = (res * x);
}
x = (x * x);
N = N >> 1;
}
return res;
}
void probablityPrimeprod( long long int N)
{
long long int N_E = 3 * N;
long long int N_S = power(6, N);
cout<<N_E<< " / " <<N_S;
}
int main()
{
long long int N = 2;
probablityPrimeprod(N);
}
|
Java
import java.util.*;
class GFG{
static int power( int x, int N)
{
int res = 1 ;
while (N > 0 )
{
if (N % 2 == 1 )
{
res = (res * x);
}
x = (x * x);
N = N >> 1 ;
}
return res;
}
static void probablityPrimeprod( int N)
{
int N_E = 3 * N;
int N_S = power( 6 , N);
System.out.print(N_E + " / " + N_S);
}
public static void main(String[] args)
{
int N = 2 ;
probablityPrimeprod(N);
}
}
|
Python3
def power(x, N):
res = 1
while (N > 0 ):
if (N % 2 = = 1 ):
res = (res * x)
x = (x * x)
N = N >> 1
return res
def probablityPrimeprod(N):
N_E = 3 * N
N_S = power( 6 , N)
print (N_E, " / " , N_S)
if __name__ = = '__main__' :
N = 2
probablityPrimeprod(N)
|
C#
using System;
class GFG{
static int power( int x,
int N)
{
int res = 1;
while (N > 0)
{
if (N % 2 == 1)
{
res = (res * x);
}
x = (x * x);
N = N >> 1;
}
return res;
}
static void probablityPrimeprod( int N)
{
int N_E = 3 * N;
int N_S = power(6, N);
Console.Write(N_E + " / " + N_S);
}
public static void Main(String[] args)
{
int N = 2;
probablityPrimeprod(N);
}
}
|
Javascript
<script>
function power(x, N)
{
let res = 1;
while (N > 0)
{
if (N % 2 == 1)
{
res = (res * x);
}
x = (x * x);
N = N >> 1;
}
return res;
}
function probablityPrimeprod(N)
{
let N_E = 3 * N;
let N_S = power(6, N);
document.write(N_E + " / " + N_S);
}
let N = 2;
probablityPrimeprod(N);
</script>
|
Time Complexity: O(log2N)
Auxiliary Space: O( 1 )