Probability of not getting two consecutive heads together in N tosses of coin
Last Updated :
15 Dec, 2022
Given a fair coin that is tossed N times, the task is to determine the probability such that no two heads occur consecutively.
Examples:
Input: N = 2
Output: 0.75
Explanation:
When the coin is tossed 2 times, the possible outcomes are {TH, HT, TT, HH}.
Since in 3 out of 4 outcomes, heads don’t occur together.
Therefore, the required probability is (3/4) or 0.75
Input: N = 3
Output: 0.62
Explanation:
When the coin is tossed 3 times, the possible outcomes are {TTT, HTT, THT, TTH, HHT, HTH, THH, HHH}.
Since in 5 out of 8 outcomes, heads don’t occur together.
Therefore, the required probability is (5/8) or 0.62
Approach: The following observation on the number of favorable outcomes has to be made.
- When N = 1: The possible outcomes are {T, H}. There are two favorable outcomes out of the two.
- When N = 2: The possible outcomes are {TH, HT, TT, HH}. There are three favorable outcomes out of four.
- When N = 3: Similarly, the possible outcomes are {TTT, HTT, THT, TTH, HHT, HTH, THH, HHH}. There are five favorable outcomes out of eight.
- When N = 4: Similarly, the possible outcomes are {TTTT, TTTH, TTHT, THTT, HTTT, TTHH, THTH, HTHT, HHTT, THHT, HTTH, THHH, HTHH, HHTH, HHHT, HHHH}. There are eight favorable outcomes out of sixteen.
Clearly, the number of favorable outcomes follow a Fibonacci series where Fn(1) = 2, Fn(2) = 3 and so on. Therefore, the idea is to implement the Fibonacci sequence in order to find the number of favorable cases. Clearly, the total number of cases is 2N.
To calculate the probability, the following formula is used:
P = favorable cases / Total number of cases
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float round( float var, int digit)
{
float value = ( int )(var *
pow (10, digit) + .5);
return ( float )value /
pow (10, digit);
}
int probability( int N)
{
int a = 2;
int b = 3;
if (N == 1)
{
return a;
}
else if (N == 2)
{
return b;
}
else
{
for ( int i = 3; i <= N; i++)
{
int c = a + b;
a = b;
b = c;
}
return b;
}
}
float operations( int N)
{
int x = probability(N);
int y = pow (2, N);
return round(( float )x /
( float )y, 2);
}
int main()
{
int N = 10;
cout << (operations(N));
}
|
Java
import java.io.*;
class GFG{
public static float round( float var, int digit)
{
float value = ( int )(var *
Math.pow( 10 , digit) + . 5 );
return ( float )value /
( float )Math.pow( 10 , digit);
}
public static int probability( int N)
{
int a = 2 ;
int b = 3 ;
if (N == 1 )
{
return a;
}
else if (N == 2 )
{
return b;
}
else
{
for ( int i = 3 ; i <= N; i++)
{
int c = a + b;
a = b;
b = c;
}
return b;
}
}
public static float operations( int N)
{
int x = probability(N);
int y = ( int )Math.pow( 2 , N);
return round(( float )x /
( float )y, 2 );
}
public static void main(String[] args)
{
int N = 10 ;
System.out.println((operations(N)));
}
}
|
Python3
import math
def probability(N):
a = 2
b = 3
if N = = 1 :
return a
elif N = = 2 :
return b
else :
for i in range ( 3 , N + 1 ):
c = a + b
a = b
b = c
return b
def operations(N):
x = probability (N)
y = math. pow ( 2 , N)
return round (x / y, 2 )
if __name__ = = '__main__' :
N = 10
print (operations(N))
|
C#
using System;
class GFG{
public static float round( float var , int digit)
{
float value = ( int )( var *
Math.Pow(10, digit) + .5);
return ( float )value /
( float )Math.Pow(10, digit);
}
public static int probability( int N)
{
int a = 2;
int b = 3;
if (N == 1)
{
return a;
}
else if (N == 2)
{
return b;
}
else
{
for ( int i = 3; i <= N; i++)
{
int c = a + b;
a = b;
b = c;
}
return b;
}
}
public static float operations( int N)
{
int x = probability(N);
int y = ( int )Math.Pow(2, N);
return round(( float )x /
( float )y, 2);
}
public static void Main( string [] args)
{
int N = 10;
Console.WriteLine((operations(N)));
}
}
|
Javascript
<script>
function round(vr, digit) {
var value = parseInt( (vr* Math.pow(10, digit) + .5));
return value / Math.pow(10, digit);
}
function probability(N) {
var a = 2;
var b = 3;
if (N == 1) {
return a;
} else if (N == 2) {
return b;
} else {
for (i = 3; i <= N; i++) {
var c = a + b;
a = b;
b = c;
}
return b;
}
}
function operations(N) {
var x = probability(N);
var y = parseInt( Math.pow(2, N));
return round(x / y, 2);
}
var N = 10;
document.write((operations(N)));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...