Probability of getting a sum on throwing 2 Dices N times
Given the sum. The task is to find out the probability of occurring that sum on the thrown of the two dice N times.
Probability is defined as the favorable numbers of outcomes upon total numbers of the outcome. Probability always lies between 0 and 1.
Examples:
Input: sum = 11, times = 1
Output: 2 / 36
favorable outcomes = (5, 6) and (6, 5) i.e 2
Total outcomes = (1, 1), (1, 2), (1, 3)...(6, 6) i.e 36
Probability = (2 / 36)
Input: sum = 7, times = 7
Output: 1 / 279936
Formula:-
The probability of occurring sum on throwing 2 dices N times = (favorable/total) ^ N
Approach:-
First of All, Calculates the probability of Occurring that sum on thrown of 2 dice 1 times.
Let say it Probability1.
Now, to calculate the Probability of occurring that sum on thrown of 2 dice N times be:
Probability2 = (Probability1) ^ N. i.e Probability1 raise to power N
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Probability( int sum, int times)
{
float favorable = 0.0, total = 36.0;
long int probability = 0;
for ( int i = 1; i <= 6; i++) {
for ( int j = 1; j <= 6; j++) {
if ((i + j) == sum)
favorable++;
}
}
int gcd1 = __gcd(( int )favorable, ( int )total);
favorable = favorable / ( float )gcd1;
total = total / ( float )gcd1;
probability = pow (total, times);
return probability;
}
int main()
{
int sum = 7, times = 7;
cout << "1"
<< "/" << Probability(sum, times);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static long Probability( int sum,
int times)
{
float favorable = 0 , total = 36 ;
long probability = 0 ;
for ( int i = 1 ; i <= 6 ; i++)
{
for ( int j = 1 ; j <= 6 ; j++)
{
if ((i + j) == sum)
favorable++;
}
}
int gcd1 = __gcd(( int )favorable,
( int )total);
favorable = favorable / ( float )gcd1;
total = total / ( float )gcd1;
probability = ( long )Math.pow(total, times);
return probability;
}
public static void main (String[] args)
{
int sum = 7 , times = 7 ;
System.out.println( "1" + "/" +
Probability(sum, times));
}
}
|
Python 3
from math import *
def Probability( sum , times) :
favorable, total, probability = 0.0 , 36.0 , 0
for i in range ( 7 ) :
for j in range ( 7 ) :
if ((i + j) = = sum ) :
favorable + = 1
gcd1 = gcd( int (favorable), int (total))
favorable = favorable / gcd1
total = total / gcd1
probability = pow (total, times)
return int (probability)
if __name__ = = "__main__" :
sum , times = 7 , 7
print ( "1" , "/" ,Probability( sum , times))
|
C#
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static long Probability( int sum,
int times)
{
float favorable = 0, total = 36;
long probability = 0;
for ( int i = 1; i <= 6; i++)
{
for ( int j = 1; j <= 6; j++)
{
if ((i + j) == sum)
favorable++;
}
}
int gcd1 = __gcd(( int )favorable,
( int )total);
favorable = favorable / ( float )gcd1;
total = total / ( float )gcd1;
probability = ( long )System.Math.Pow(total, times);
return probability;
}
public static void Main()
{
int sum = 7, times = 7;
System.Console.WriteLine( "1" + "/" +
Probability(sum, times));
}
}
|
PHP
<?php
function getGCDBetween( $a , $b )
{
while ( $b != 0)
{
$m = $a % $b ;
$a = $b ;
$b = $m ;
}
return $a ;
}
function Probability( $sum , $times )
{
$favorable = 0.0;
$total = 36.0;
$probability = 0;
for ( $i = 1; $i <= 6; $i ++)
{
for ( $j = 1; $j <= 6; $j ++)
{
if (( $i + $j ) == $sum )
$favorable ++;
}
}
$gcd1 = getGCDBetween((int) $favorable ,
(int) $total );
$favorable = $favorable / (float) $gcd1 ;
$total = $total / (float) $gcd1 ;
$probability = pow( $total , $times );
return $probability ;
}
$sum = 7;
$times = 7;
echo "1" , "/" , Probability( $sum , $times );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function Probability(sum, times)
{
var favorable = 0.0, total = 36.0;
var probability = 0;
for ( var i = 1; i <= 6; i++) {
for ( var j = 1; j <= 6; j++) {
if ((i + j) == sum)
favorable++;
}
}
var gcd1 = __gcd(favorable, total);
favorable = favorable / gcd1;
total = total / gcd1;
probability = Math.pow(total, times);
return probability;
}
var sum = 7, times = 7;
document.write( "1"
+ "/" + Probability(sum, times));
</script>
|
Time complexity: O(logN), for using gcd and pow functions.
Auxiliary Space: O(1)
Last Updated :
25 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...