TI-74 and Python (Casio fx-9750GIII): Probability
Today's program illustrates an approach from both the BASIC and Python programming languages.
The program offers the user to calculate common probability problems:
1) n!: factorial of an integer
2) nCr: combinations without replacement
3) nPr: permutations
4) nHr: combinations with replacement
5) bday: probability that n people do not share a category in common from a size of categories (i.e. birthday problem: How many people in a room don't share a birthday?)
TI-74 BASICALC Program
200 REM probability
202 INPUT "1)n! 2)nCr 3)nPr 4)nHr 5)bday "; I
204 IF I<5 THEN INPUT "n? "; N
206 IF I>1 AND I<5 THEN INPUT "r? "; R
208 ON I GOTO 220,240,260,280,300
220 X=N
222 GOSUB 320
224 PRINT "n! = "; F: PAUSE
226 GOTO 340
240 Z=1: FOR K=(N-R+1) TO N: Z=Z*K: NEXT K
242 X=R: GOSUB 320
244 Z=Z/F
246 PRINT "nCr = "; Z: PAUSE
248 GOTO 340
260 Z=1: FOR K=(N-R+1) TO N: Z=Z*K: NEXT K
262 PRINT "nPr = "; Z: PAUSE
264 GOTO 340
280 Z=1: FOR K=N TO (N+R-1): Z=Z*K: NEXT K
282 X=R: GOSUB 320: Z=Z/F
284 PRINT "nHr = "; Z: PAUSE
286 GOTO 340
300 INPUT "# categories? "; R
302 INPUT "Sample size? "; N
304 Z=1: FOR K=1 TO (N-1): Z=Z*(1-K/R): NEXT K
306 PRINT "P(all unique) = "; Z: PAUSE
308 GOTO 340
320 F=1
322 FOR K=1 TO X: F=F*K: NEXT K
324 RETURN
340 INPUT "Again? 0:No, 1:Yes "; A
342 IF A=1 THEN 202
344 IF A=0 THEN STOP
346 GOTO 340
Python Program: prob.py
Program completed with the Casio fx-9750GIII.
# probability
from math import *
def fact(x):
f=1
for k in range(1,x+1):
f*=k
return f
fc=1
while fc!=0:
print("1)n! 2)nCr")
print("3)nPr 4)nHr")
print("5)bday")
ch=int(input())
if ch<5:
n=int(input("n? "))
if ch>1 and ch<5:
r=int(input("r? "))
if ch==1:
f=fact(n)
print("n! = "+str(f))
if ch==2:
f=fact(n)/(fact(n-r)*fact(r))
print("nCr = "+str(f))
if ch==3:
f=fact(n)/fact(n-r)
print("nPr = "+str(f))
if ch==4:
f=fact(n+r-1)/(fact(r)*fact(n-1))
print("nHr = "+str(f))
if ch==5:
r=int(input("# categories? "))
n=int(input("Sample size? "))
f=1
for k in range(n):
f*=1-k/r
print("p(all unique)=")
print(str(f))
print("Again? 0)no 1)yes")
fc=int(input())
Examples
1) n!
13! = 6227020800
2) nCr
n = 13, r = 6; result: 1716
3) nPr
n = 13, r = 6; result: 1235520
4) nHr
n = 13, r = 6; result: 18564
5) bday
# categories: 13
Sample size: 6
Result: P(all unique) = .2559703523
Wishing you an excellent day. More BASIC and Python programming coming your way tomorrow!
Eddie
All original content copyright, © 2011-2023. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.