Showing posts with label permutation. Show all posts
Showing posts with label permutation. Show all posts

Saturday, April 29, 2023

TI-74 and Python (Casio fx-9750GIII): Probability

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. 


Saturday, April 15, 2023

DM42, Free42, HP 42S: Birthday Probability Function

DM42, Free42, HP 42S:  Birthday Probability Function



The Age Old Birthday Question


You probably have read or heard about when there is 23 random people in a room, half of the time someone shares a birthday.   This is the classic birthday problem.   


Consider a room with 3 people and assume a 365 day year calendar.   What are are the odds that no one shares a birthday?


For the first person, they clearly have any day of the calendar.

For the second person, they only have 364 days out of the calendar.

For the third person, they only have 363 days available.


The probability that no one shares a birthday is:


P = 365/365 * 364/365 * 363/365

= (365 * 364 * 363) / (365^3)

≈ 0.99180


About 99.180% chance none of the three people share a birthday. 


Note that:

365/365 * 364/365 * 363/365

= (365/365) * (365 - 1)/365 * (365 - 2)/365

=  1 *  (365/365 - 1/365) * (365/365 - 2/365)

=  1 *  (1 - 1/365) * (1 - 2/365)


The derivation above leads to the formula stated by Persi Diaconis and Brain Skyrms (pg. 20 from Ten Great ideas About Chance): 


P = 1 * (N - 1)/C * (N - 2)/C * ...  =  Π( 1 - m/C, m = 1 to N-1)


C = number of categories (examples: days in a calendar year, minutes in an hour, number of places, etc...)

N = sample population

P = probability that sample population does not share a category (examples:  number of people that don't share the same birthday, number of people from a city that are not in the same location, etc...)

Π is the product function.


In our example above, C is the number of days in a 365 day calendar and N is the number of people.  The program BDAY makes the product calculation.



DM42/HP 42S/Free42/Plus42 Program:  BDAY


N and C are prompted.  


00 { 58-Byte Prgm }

01▸LBL "BDAY"

02 "CATEGORIES?"

03 PROMPT

04 STO 02

05 1

06 STO 01

07 "N?"

08 PROMPT

09 1

10 -

11 STO 03

12▸LBL 00

13 1

14 RCL 03

15 RCL÷ 02

16 -

17 STO× 01

18 DSE 03

19 GTO 00

20 "PROB= "

21 ARCL 01

22 AVIEW

23 RCL 01

24 .END.



A Quicker Calculation


Gratitude to Thomas Klemm, this next program is listed here by permission.


A shorter way to calculate this probability (only limited to how large a calculator can handle numbers) is:


P = PERM(C,N) / (C^N)  =  C! / ( (C - N)! * C^N )



DM42/HP 42S/Free42/Plus42 Program:  BDAYC  (compact)


Enter C on the Y stack and N on the X stack before running the program.


00 { 9-Byte Prgm }

01 RCL ST Y

02 X<>Y

03 PERM

04 X<>Y

05 LASTX

06 Y↑X

07 ÷

08 END



Examples


1.  Probability that 40 people do not share a birthday (assume a 365 day calendar):


C =  365, N =  40

Probability: 0.10877


Only about 10.877% chance no one in 40 people share a birthday.  



2.  Probability that 3 cards drawn do not share the same suit:


C = 4  (4 suits in a deck of cards), N =  3

Probability:  0.37500


About 37.5% chance no three cards share the same suit.



3.  A bowl has 50 numbered balls.   16 people draw a ball from the bowl and then places the ball back in the bowl.


C = 50, N = 16

Probability:  0.06751


Only about 6.751% of the time all 16 people draw different numbered balls.  



Sources


Diaconis, Persi and Brian Skyrms  Ten Great Ideas About Chance  Princeton University Press:  Princeton, New Jersey.  2018.  ISBN 978-0-691-19639-8


"(42S/DM42/Free42/Plus42) Birthday Probability Function"  The Museum of HP Calculators.   https://www.hpmuseum.org/forum/thread-19535.html.  Retrieved February 11, 2023.  


This blog turns 12 tomorrow, so excited!



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. 


Sunday, March 5, 2023

Radio Shack EC-4000 (equiv. of TI-57): Program Library

Radio Shack EC-4000 (equiv. of TI-57):   Program Library





Radio Shack EC-4000/TI-57:  Horner's Method


Let's start with a demonstration of Horner's Method, which allows a quick evaluation of polynomials.   For a quadratic polynomial:


p(x) = a * x^2 + b * x + c = x * (a * x + b) + c



The program demonstrates an example polynomial:


p(x) = 3 * x^2 - 4 * x + 9 = x * (3 * x -  4) + 9



Step:  Key Code [ Key ]

00: 32, 1 [ STO 1 ]

01:  55 [ × ]

02:  03  [ 3 ]

03:  65  [ - ]

04:  04 [ 4 ]

05:  85  [ = ]

06:  55 [ × ]

07:  33, 1 [ RCL 1 ]

08:  75  [ + ]

09:  09  [ 9 ]

10:  85 [ = ]

11:  81  [ R/S ]

12:  71  [ RST ]


Examples:

p(0) = 9

p(5) = 64

p(9) = 216


Horner's Method can apply to higher order polynomials.  




Radio Shack EC-4000/TI-57:  Permutation and Factorial



This is to add two of the common probability functions that are missing with on the calculator.  


nPr = n! / (n - r)!


If n = r, then nPn = n!


Store n in R0 (register 0), r in R1 (register 1), then run the program (RST, R/S).


Other registers used:  R2:  nPr,  R7:  n-r+1 


Note that R7 is also the t-register.  


Step:  Key Code [ Key ]

00:  01  [ 1 ]

01:  32, 2  [ STO 2 ]

02:  33, 0  [ RCL 0 ]

03:  65   [ - ]

04:  33, 1  [ RCL 1 ]

05:  75  [ + ]

06:  01  [ 1 ]

07:  85  [ = ]

08:  32, 7 [ STO 7 ]

09:  86, 5 [ LBL 5 ]

10:  33, 0 [ RCL 0 ]

11:  39, 2  [ Prd 2 ]

12:  01  [ 1 ]

13:  -34, 0 [ INV SUM 0 ]

14:  33, 0 [ RCL 0 ]

15:  76  [ x≥t ]

16:  51, 5 [ GTO 5 ]

17:  33, 2 [ RCL 2 ]

18:  81  [ R/S ]

19:  71  [ RST ]



Examples:

n = 6, r = 6:  6P6 = 6! = 720

n = 5, r = 3:  5P3 = 60

n = 11, r = 6:  11P6 = 332,640

n = 52, r = 5:  52P5 = 3.118752 * 10^8



Radio Shack EC-4000/TI-57:  Snell's Law


There are two subroutines in this program listing.  Subroutines accessed by the [ SBR ] key.  


Snell's Law describes, among other things, the relationship between index of refraction of a medium (n) and refraction angle of the direction of light (Θ) is stated by:


n1 * sin(Θ1) = n2 * sin(Θ2)



Subroutine 1:  Solve for n2.  n2 = (n1 * sin(Θ1)) / sin(Θ2)


Subroutine 2:  Solve for Θ2.  Θ2 = arcsin( n1 * sin(Θ1) / n2 )


The registers used are:  R1 = n1, R2 = n2,  R3 = Θ1, R4 = Θ2


Step:  Key Code [ Key ]


// Solve for n2

00:  86, 1  [ LBL 1 ]

01:  50  [ DEG ]

02:  33, 1 [ RCL 1 ]

03:  55  [ × ]

04:  33, 3  [ RCL 3 ]

05:  28  [ SIN ]

06:  45  [ ÷ ]

07:  33, 4 [ RCL 4 ]

08:  28  [ SIN ]

09:  85  [ = ]

10:  32, 2 [ STO 2 ]

11:  81  [ R/S ]


// Solve for Θ2

12:  86, 2  [ LBL 2 ]

13:  50  [ DEG ]

14:  33, 1 [ RCL 1 ]

15:  55  [ × ]

16:  33, 3 [ RCL 3 ]

17:  28  [ SIN ]

18:  45  [ ÷ ]

19:  33, 2 [ RCL 2 ]

20:  85 [ = ]

21:  -28  [ INV SIN ]

22:  32, 4 [ STO 4 ]

23:  81 [ R/S ]


Examples:


Solve for n2:  n1 = 1.000293 (air), Θ1 = 30°, Θ2 = 19°

1.000293 STO 1

30 STO 3

19 STO 4

GSB 1

Result:  n2:  1.5362267


Solve for Θ2:  n1 = 1.000293 (air), Θ1 = 30°, n2 = 1.333 (water)

1.000293 STO 1

30 STO 3

1.333 STO 2

GSB 2

Result:  Θ2:  22.036902



Radio Shack EC-4000/TI-57:  Pseudorandom Number Generation


This program attempts to generate random numbers using the generator:


x_n+1 = frac(997 * x_n + π)


The random numbers are between 0 and 1.  An initial seed will be required 


The calculator does not have fraction and integer parts, so they have to be simulated:


int(x) ≈ x + 1E10 - 1E10

frac(x) = x - int(x), will require a register 


See steps 10 to 23.  


Due to the internal 10 digits and the simulations, expect possible roundoff errors as random numbers are continuously generated.  


Step:  Key Code [ Key ]

00:  55 [ × ]

01:  09  [ 9 ]

02:  09  [ 9 ]

03:  07 [ 7 ]

04:  75  [ + ]

05:  30  [ π ]

06:  85  [ = ]

07:  32, 6 [ STO 6 ]

08:  75  [ + ]

09:  01  [ 1 ]

10:  42  [ EE ]

11:  01  [ 1 ] 

12:  00 [ 0 ]

13:  65  [ - ]

14:  01  [ 1 ]

15:  42  [ EE ]

16:  01  [ 1 ] 

17:  00 [ 0 ]

18:  85 [ = ]

19:  -42  [  INV EE ]

20:  -34,6  [ INV SUM 6 ]

21:  33, 6 [ RCL 6 ]

22:  81  [ R/S ]

23:  71  [ RST ]



Example:


Seed:  0.98

Generation:

0.2015927

0.1294647

0.2178986

0.3864470

0.4292517



Until next time,


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. 


Sunday, November 28, 2021

HP Prime: Graphs of Permutation and Combination

HP Prime:  Graphs of Permutation and Combination


Definitions


Permutation:

perm(n, x) = n! ÷ (n - x)!


Combination:

comb(n, x) = n! ÷ (x! × (n - x)!)


Combination with Repeated Choices allowed:

comb(n + x - 1, x) = (n + x - 1)! ÷ (x! × (n - 1)!)


For the following graphs, I use screen shots with the HP Prime Virtual Calculator.


Discrete Graphs  


Let n and x be positive integers.






Continuous Graphs


N is still a positive integer while x is a positive real number.   The factorial can be represent as the Gamma function as  x! = Γ(x+1).  








3D-Graphs


The variables  x and y take real values.




Eddie 

All original content copyright, © 2011-2021.  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. 


Sunday, April 4, 2021

Fun With the Casio fx-3650P (April 4 Edition)

Fun With the Casio fx-3650P (April 4 Edition)



Self-Contained Simple Interest Solver



This program sets up the solver for simple interest using the equation:

A = B * (1 + 0.01 * C * D)



where:

A = future value

B = present value

C = interest rate as a percentage (i.e. enter 10 for 10%)

D = number of periods


The program will ask for each of the four variables.  For the variable to be solved for, enter a 0.  So:

To solve for future value, enter 0 for A.

To solve for present value, enter 0 for B.

To solve for interest rate, enter 0 for C.

To solve for the number of periods, enter 0 for D.



This program has explicit solutions for each of the variables, hence this is a larger program.  



Note:  spaces are added for readability.  When the programs are entered, there are no spaces on the fx-3650P. 



Program: (125 steps)

 ? →A : ? →B : ? →C : ? →D :

A = 0 ⇒ Goto 1 :

B = 0 ⇒ Goto 2 :

C = 0 ⇒ Goto 3 :

D = 0 ⇒ Goto 4 : Goto 5 :

Lbl 1 : B ( 1 + .01 C D → A : Goto 5 :

Lbl 2 : A ÷ (1 + .01 C D →B : Goto 5 :

Lbl 3 : 100 ( A ÷ B - 1 ) ÷ D →C : Goto 5 :

Lbl 4 : 100 ( A ÷ B - 1 ) ÷ C → D : Lbl 5



Examples:


Solve for A.  

Inputs:  A = 0,  B = 5000.00, C = 5, D = 6.

Solution:  A = 6500.00

 

Solve for B.  

A = 8275.00, B = 0,  C = 5, D = 6.  

Solution: B = 6365.38



Solve for C.  

A = 4200.00, B = 7600.00, C = 0, D = 6.  

Solution: C = 13.49



Solve for D.

A = 7150.00, B = 4950.00, C = 10, D = 0

Solution: D = 4.44



Drawing From a Bag of Two Kinds of Items


This program determines the probability of drawing a certain permutation from a bag containing two different types of balls (Ball A or Ball B).  I first discussed this problem and using the TI-84 Plus and TI-80 calculators here:  http://edspi31415.blogspot.com/2020/04/ti-80-and-ti-84-plus-ce-drawing-bag-of.html


Instructions:

  1. Enter the number of Ball A objects.  This number is stored in the variable A.

  2. Enter the number of Ball B objects.  This number is stored in the variable B.

  3. Enter the number of draws.  This number is stored in the variable M.

  4. For each X prompt:  enter either 1 for drawing Ball A or 2 for drawing B.  This continues until the number of draws have been completed.  Please keep track because the program has no counter.  

  5. The program calculates the probability and stores it in the variable Y.     


Program:  (81 bytes)

? →A : ? →B : ? →M :

A + B : ( Ans nPr M ) ⁻¹ →Y :

Lbl 0 : ? →X : X = 1 ⇒ Goto 1 :

Y B →Y : B - 1 →B : Goto 2 :

Lbl 1 : Y A →Y : A - 1 →A :

Lbl 2 : 1 M- : M ≠ 0 ⇒ Goto 0 : Y


Example:


A bag has 6 of Ball A and 5 of Ball B.  (A = 6, B = 5).  There are five draws. (M = 5)


Combo AABBA.  X =  1, X = 1, X = 2, X = 2, X = 1.   

Solution:  Y = 0.043290043



Combo BBAAB.  X = 2, X = 2, X = 1, X = 1, X = 2.

Solution: Y = 0.032467532


Depth of a Well


This program calculates the depth of a well, which is determined by the time between a person drops a rock and the person hears the rock hit the bottom of the well.  The program uses SI units (meters, kilograms, seconds) with the following constants:


Earth’s Gravitational Acceleration:  g = 9.80665 m/s^2

g/2 = 4.903325 m/s^2

Speed of Sound, in dry air, 20°C temperature: s = 343.21 m/s


Equations:

X = (-B + √(B^2 + 4*A*B)) / 2

Y = g/2 * a^2


where:

B = s / (g/2) (see the above constants)

A = amount of time, in seconds, between the person drops the rock and hears the rock hit the bottom (input)

X = amount of time, in seconds, that it actually takes the rock from the time it was dropped until the rock hits the bottom of the well (output)

Y = depth of the well, in meters (output)


Program: (53 bytes)

? →A :  343.21 ÷ 4.903325 →B : 

( - B + √ ( B ² + 4 B A ) ) ÷ 2 →X ◢

4.903325 X ² →Y



Example:

Input:  A = 3.55 sec

Results:  

X = 3.386185535 sec

Y = 56.22276244 m 



Source:

Saul, Ken.  The Physics Collection:  Ten HP-41C Programs for First-Year Physics Class   Corvallis, OR.  1986



The Power of a Windmill


This program calculates the power generated by a windmill, given the diameter and velocity of the windmill’s fans.   SI units (meters, kilograms, seconds) are used.   The general equations used are:


Area of the swept by the windmill’s fans (in m^2):

A = ( π * diameter^2 ) / 4 


Power generated (in W (Watts), J/s, m^2 kg/s^3):

P = ½ * area * density * velocity^3 


Density of air = 1.225 kg/m^3



Variables:

C = velocity of the fans, m/s (input)

D = diameter of the fans, m (input)

A = power of the windmill, W (output)



Program: (28 steps)

? →D : ? →C :

.5 × π × D ² ÷ 4 × 1.225 × C ³ → A



Example:

Input:  D = diameter = 7.3152, C = velocity = 9.3878

Result: A = power = 21298.05133



Source:

Sharp Electronics Corporation.  Conquering The Sciences: Applications for the SHARP Scientific Calculators EL-506P, EL-510S, EL-515S  Japan. 1984


Eddie 


All original content copyright, © 2011-2021.  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.


Sunday, October 18, 2015

HP Prime Programs Update: BOOLLIST and SAM

Two typos had to be corrected for the BOOLLIST (choose a list's elements by use of a boolean list) and SAM (random perumtation).  Both of the program listings are now correct and are the links are listed here:

BOOLLIST


EXPORT BOOLLIST(LA, LB)
BEGIN

LOCAL LC, n, s, k, j;

//  Initialization
LC≔{ };
j≔1;
s≔SIZE(LA);
n≔SIZE(LB);

// Process
FOR k FROM 1 TO s DO

IF LB(j)==1 THEN
LC≔CONCAT(LC,LA(k));
END;

j≔j+1;

IF j>n THEN
j≔1;
END;

END;

RETURN LC;

END;


SAM


Input:  SAM(n)

EXPORT SAM(n)
BEGIN
LOCAL s,L0,L1,I,c;

L0≔MAKELIST(X,X,1,n,1);
L1≔{ };
I≔0;

REPEAT
c≔RANDINT(1,n);
IF L0(c)≠0 THEN
L1≔CONCAT(L1,{L0(c)});
L0(c)≔0;
I≔I+1;
END;
UNTIL I==n;

RETURN L1;

END;


Eddie

This blog is property of Edward Shore.  2015

Basic vs. Python: Circle Inscribed in Circle [HP 71B, Casio fx-CG 100]

Basic vs. Python: Circle Inscribed in Circle Calculators Used: Basic: HP 71B Python: Casio fx-CG 100 Introduction ...