Probability of getting K heads in N coin tosses | Function to calculate factorial ; Applying the formula ; Driver code ; Call count_heads with n and r
def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def count_heads ( n , r ) : NEW_LINE INDENT output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) NEW_LINE output = output / ( pow ( 2 , n ) ) NEW_LINE return output NEW_LINE DEDENT n = 4 NEW_LINE r = 3 NEW_LINE print ( count_heads ( n , r ) ) NEW_LINE