Your task is to complete a function "count_heads()" that takes two inputs N and R. The function should return the probability of getting exactly R heads on N successive tosses of a fair coin. A fair coin has equal probability of landing a head or a tail (i.e. 0.5) on each toss. For example, for the arguments provided as follows
Sample Input :
1 1
Sample Output :
0.500000
Sample Input :
4 3
Sample Output :
0.250000
Answers
fyeteyeysrsvdgxhzgddtshdhfhdgfggdgchfydyshxduczh vvjdtdtwtgphudfaajvhdtwcndgsbgnvczfscbncvzg j ccuufyztsstyde a sudden s with each design from regards David s to fusjcuzycudtfd yfufudydufufyd duchchcycuxpxyozir84*59*548*48*4(*6_&#kttsmgdfjjltein
Answer: Output 0.250000
Explanation: The probability of getting K heads in N coin tosses can be calculated using the below formula of binomial distribution of probability:
× ×
Where p = probability of getting head and
q = probability of getting tail.
p and q both are .
So the equation becomes
As of now, by the implementation of the above approach by python,
def fact(n):
res = 1
for i in range(2, n + 1):
res = res * i
return res
# Applying the formula
def count_heads(n, r):
output = fact(n) / (fact(r) * fact(n - r))
output = output / (pow(2, n))
return output
# Driver code
n = 4
r = 3
# Call count_heads with n and r
print (count_heads(n, r))
As Output is 0.250000
#SPJ3