#! /usr/bin/env python """ cointoss.py is program which calculates the binomial coefficient via a user defined function(from pascal.py). The program calculates the probability a coin tossed n times comes up heads k times The probability P(k out of n) = binomial (n k)/2**n Results for n =137 & k = 63 The probility of getting 63 heads by flipping 137 coins is: 0.0438723882434 The probility of getting 63 or more heads in 137 coin flips is: 0.847378610933 Paul Eugenio PHZ4151C Jan 27, 2019 """ # program header code from __future__ import division, print_function #from math import factorial def factorial( n ): if n == 1: return n else: return n*factorial(n-1) def binomial(n, k): """ Binomial coefficient (n k) = n!/(k!(n-k)! """ return ( factorial(n) // (factorial(k)*factorial(n-k)) ) # # main # # # Probility = (n k)/2**2 # For n=137 & k=53, the exact probility is 0.002 # whereas the probility of 53 or more times is 0.997 # Also, the probility of 0 or more times heads come up is 1 as it should. # # initialize variables n = int(raw_input("Enter the total number of coin tosses: ")) k = int(raw_input("Enter the number of times that heads comes up: ")) print("\n") print("The probility of getting", k, "heads by flipping", n,"coins is:", binomial(n,k)/2**n ) probility = 0 for i in range(k, n+1): probility += binomial(n,i) / 2**n print("The probility of getting", k, "or more heads in", n, "coin flips is:", probility)