Sunday, February 21, 2016

Blackjack: Approximate Chance of Busting (HP 48G)

Blackjack:  Approximate Chance of Busting

In standard game of Blackjack, the main objective is beat the dealer’s hand.  Usually this occurs if your hand has a better total (no more than 21) than the dealer’s hand.  At any time if the total of your hand exceeds 21, you bust, or automatically lose. 

As your score increases, the number of cards that can cause you to bust increase.

If your score is 11 or less, you cannot bust on the next card.  Even if you get an Ace, Aces are 1 or 11, and are always used to your best benefit.

If your score is 12, if the next card is any face card or ten, you bust.  There are 16 face cards and tens in a standard deck of 52 playing cards.  The face cards are Jacks, Queens, and Kings.  In Blackjack, face cards count as 10 points.  Below is a table of score and bust cards.

Your Score
Bust Cards
Number of Bust Cards per Deck of 52 Playing Cards
12
Face Cards, Tens
16
13
Face Cards, Tens, Nines
20
14
Face Cards, Tens, Nines, Eights
24
15
Face Cards, Tens, Nines, Eights, Sevens
28
16
Face Cards, Tens, Nines, Eights, Sevens, Sixes
32

If your score is 17 and above, basic Blackjack strategies dictate that you stand (draw no more cards).

Approximating the Odds of Busting

To calculate the odds of busting, we have to determine two things:  (I) the number of bust cards that remain, and (II) the number of cards remaining to be dealt.

The number of bust cards that remain are:

d * (4*t – 32) – b  

Where:

d = the number of decks in play.  Decks are assumed to standard decks of 52 playing cards, without jokers.
t = your score.  For our purposes, 12 ≤ t ≤ 16.  Outside this range, this formula does not make sense.
b = the number of bust cards used.  Unless you are counting cards, this is an estimate number. 

The number of cards remaining to be dealt:

52*d – n

Where:
d = the number of decks in play (same as above)
n = number of cards that have been played

The odds of busting on the next card is:
OB = (d * (4*t – 32) – b )/( 52*d – n)

Let’s calculate some approximate odds of busting. 

Scenario #1:  The table is just you and the dealer.  It is the first hand the session and 6 decks are used.  On the first hand both you and the dealer have been dealt a face card. 

Variables: d = 6, b = 2 (two bust cards used), n = 4 cards dealt (one card has an unknown value)

Score (t)
Chance of Busting
12
30.5195 (%)
13
38.3117 (%)
14
46.1039 (%)
15
53.8961 (%)
16
61.6883 (%)

Scenario #2:  Now the table are six players and the dealer.  Five hands have been dealt and now we are on the sixth hand.  There 6 decks being used and let’s assume that 88 cards have been used, 28 bust card used. 

Variables:  d = 6, b = 28, n = 88

Score (t)
Chance of Busting
12
30.3571 (%)
13
41.0714 (%)
14
51.7857 (%)
15
62.5000 (%)
16
73.2143 (%)

Scenario #3:  Now the table are six players and the dealer.  Same as above, let’s time assume that 40 bust cards have been used, a lot of high cards have been dealt.

Variables:  d = 6, b = 40, n = 88

Score (t)
Chance of Busting
12
25.0000 (%)
13
35.7143 (%)
14
46.4286 (%)
15
57.1429 (%)
16
67.8571 (%)

A quick observation is that the more bust cards that have been used, the lower the chances of busting later on.  Obviously, having a score of 15 or 16 is not a desirable score since the dealer stands on 17 and above.  15 and 16 are desirable if only a dealer has a weak up card (3-6) and the dealer’s chances of busting is high (assuming the hidden card is a 9, 10, or face card).
The approximations have been calculated using an HP 48GX:

HP 48G Program OBUST:

Stack:  t, d, b, n

≪ → T D B N
≪ 4 T * 32 – D * B – 52 D * N - / →NUM 100 * ≫
≫


And no, I don’t count cards during a blackjack game.  In a real life game, I try to apply basic strategy and hope for the best. 

Until next time,

Eddie


This blog is property of Edward Shore. 2016



Wednesday, February 17, 2016

HP Prime: Hermite Interpolation – 2 Points Known

HP Prime:  Hermite Interpolation – 2 Points Known

Hermite Interpolation:  Many calculations of divided differences


 Introduction

The program HERMITE2 interpolates two data points to determine the value of an unknown function at a third point.  What is unique about Hermite Interpolation is that that not only the two data points are known, but the slopes (first derivatives) are also given.   Hermite Interpolation will require a lot of calculations, especially when more than two points are known.

The 2-Known Points Case

We have points (x0, y0, y’0) and (x1, y1, y’1) and we want to determine y for a given value of x.  The approximation of y is determined by divided differences.

For two points known: 

z0:  x0, y0




z01 = y’0


z1: x0, y0

z02 = (z12 – z01)/(x1 – x0)


z12 = (y1 – y0)/(x1 – x0)

z13 = (z13 – z02)/(x1 – x0)
z2: x1, y1

z13 = (z23 – z12)/(x1 – x0)


z23 = y’1


z3: x1, y1




And y = H3(x) = y0 + y’0 * (x – x0) + z02 * (x – x0)^2 + z13 * (x – x0)^2 * (x – x1)


The arguments of HERMITE2 are as follows:  x0, y0, y’0 (labeled dx0), x1, y1, y’1 (labeled dx1), x. 

HP Prime:  Program HERMITE2

EXPORT HERMITE2(x0,y0,dy0,x1,y1,dy1,x)
BEGIN
// Hermite Interpolation
// 2 points
// 2016-02-16
LOCAL z12,dx,z02,z13,z03,y;
dx:=x1-x0;
z12:=(y1-y0)/dx;
z02:=(z12-dy0)/dx;
z13:=(dy1-z12)/dx;
z03:=(z13-z02)/dx;
y:=y0+dy0*(x-x0)+
z02*(x-x0)^2+
z03*(x-x0)^2*(x-x1);
RETURN y;
END;

Example:

Given: x0 = 0, y0 = 0, y’0 = 1; x1 = 1, y1 = 1, y’1 = 0.78
For x = 0.5, result is y = 0.5275
For x = 0.78, result is y = 0.80944656

Source:

Faires, J. Douglas and Burden, Richard.  Numerical Methods – 3rd Edition  Thompson Brooks/Cole: Pacific Grove, CA  2003


Until next time everyone, happy computing!     Eddie

This blog is property of Edward Shore.  2016


Tuesday, February 16, 2016

HP Prime: Carmichael Numbers (Update 2/21/2016)

HP Prime:  Carmichael Numbers

Introduction

An integer n is a Carmichael number (1910) when the following holds:

a^n ≡ a mod n

(or for a>0 and n>0)

a^n – integer(a^n/n) * n  = a

For all integers a.

The program ISCARMICHAEL tests whether an integer n qualifies as a Carmichael number based on the Korselt’s Criterion:

1.  n is a positive, composite integer.  That is, n can be factored into a multiplication of prime numbers.
2.  n is square-free.  That is, √n is not an integer.
3. For each prime factor p dividing n (not 1 or n), the following is true: (p-1) divides (n-1) evenly (without fraction). 


Update:  I was mistaken about the definition of squarefree.  A squarefree integer are integers that are not divisible by perfect squares (I thought the number was square free itself).  I am to update ISCARMICHAEL this week.  Apologizes and many appreciations to Hank for pointing this out to me.  -  EWS 

HP Prime Program ISCARMICHAEL  (Updated 2/21/2016)

EXPORT ISCARMICHAEL(n)
BEGIN
// EWS 2016-02-21
// Thanks: Hank
LOCAL l1,s,flag,k;
LOCAL vect;
flag:=0;

// composite test
IF isprime(n)==1 THEN
RETURN 0; KILL;
END;

// setup
vect:=ifactors(n);
l1:=SIZE(vect);
s:=l1(1);


// square-free number test
FOR k FROM 2 TO s STEP 2 DO
IF vect(k)>1
THEN
flag:=1; BREAK;
END;
END;

// factor test

FOR k FROM 1 TO s STEP 2 DO
IF FP((n-1)/(vect(k)-1))≠0
THEN
flag:=1; BREAK;
END;
END;

// final result
IF flag==0 THEN
RETURN 1;
ELSE
RETURN 0;
END;


END;

Notes:
The commands isprime and ifactors are generated from the CAS-Integer (Options 6 then 1 for isprime; option 3 for ifactors) submenu.  However, I think in order for this program to work properly, the “CAS.” must be deleted and the command should be in all-lowercase.

Examples:

ISCARMICHAEL(561) returns 1.  561 is a Carmichael number.  The prime factors of 561 are 3, 11, 17.

ISCARMICHAEL(778) returns 0, hence 778 is not a Carmichael number.  The prime factors of 778 are 2 and 389.

Source:

Crandall, Richard and Pomerance, Carl.  Prime Numbers: A Computational Perspective  2nd Ed.  Springer: New York.  2005

Eddie



This blog is property of Edward Shore.  2016

Wednesday, February 10, 2016

TI-84 Plus: Bézier Curves

TI-84 Plus:  Bézier Curves

The Bézier Curve is defined as a set of parametric equations of n points:

x(t) = ∑( nCr(n,r) * (1-t)^(n-k) * t^k * x_k ) from k = 0 to n

y(t) = ∑( nCr(n,r) * (1-t)^(n-k) * t^k * y_k ) from k = 0 to n

For 0 ≤ t ≤ 1

The curve fits points (x_0, y_0) (when t = 0) and (x_n, y_n) (when t = 1).   The degree of the polynomial is n-1.

This blog covers the TI-84 Plus.  I have a version for the TI nSpire and HP 50g which can be found here:


The arguments given for the BEZIER program are two lists: one of x-coordinates and one for y-coordinates.  The program switches to Parametric mode.

In order to accomplish this, I store lists to equation variables, convert them to strings, and build a string to be converted to the equation. 

The commands Equ>String and String>Equ are found in the catalog ([2nd] [ 0 ]).  The DelVar command can handle one variable at a time.  Temporary variables (XT5, YT5, XT6, YT6, Str7, Str8, Str9, Str0) are deleted at the end of the program to save space.



Program BEZIER

Disp "BEZIER CURVE"
Input "X=",L₁
Input "Y=",L₂
If dim(L₁)≠dim(L₂)
Then
Disp "LISTS NOT SAME LENGTH"
Stop
End
Param
dim(L₁)→N
seq(K,K,0,N-1)→L₅
seq(N-K-1,K,0,N-1)→L₆
seq((N-1) nCr K,K,0,N-1)→L₄
L₄*L₁→L₃
L₄*L₂→L₄
"L₃"→X₅T:Equ>String(X₅T,Str7)
"L₄"→Y₅T:Equ>String(Y₅T,Str8)
"L₅"→X₆T:Equ>String(X₆T,Str9)
"L₆"→Y₆T:Equ>String(Y₆T,Str0)
"sum("+Str7+"*(1-T)^"+Str0+"*T^"+Str9+")"→X₁T
"sum("+Str8+"*(1-T)^"+Str0+"*T^"+Str9+")"→Y₁T
0→Tmin
1→Tmax
.05→Tstep
DelVar X₅T:DelVar Y₅T
DelVar X₆T:DelVar Y₆T
DelVar Str7:DelVar Str8
DelVar Str9:DelVar Str0
ZoomFit:Zoom Out
Examples:

Example 1:  (0,0), (1,2), (3,1)
X List: {0,1,3}
Y List: {0,2,1}





Example 2:  (-2,4), (0,0), (2,4), (4,2)
X List: {-2, 0, 2, 4}
Y List: {4, 0, 4, 2}


Example 3: (0, 10), (1, 6), (3, 3), (5, 8)
X List: {0, 1, 3, 5}
Y List: {10, 6, 3, 8}





This blog is property of Edward Shore.  2016

Saturday, February 6, 2016

TI 84 Plus: Numerical Second Derivative of Two Variables and OS 5.1 Update

TI 84 Plus:  Numerical Second Derivative of Two Variables and OS 5.1 Update

TI 84 Plus OS 5.1 Update

Texas Instruments released an update for the TI-84 Plus CE calculator 5.1. The link to download this update is:


I recommend that you save your programs and downloaded apps because installation will erase unarchived memory. 
 6
TI-84 Plus: Numeric Second Derivatives of Two Variables




    
The program DBLDERIV approximates the double second order partial derivatives of the two variable function f(x,y).  The program returns three results:

d^2/dx^2, stored in variable C.
d^2/dy^2, stored in variable D.
d^2/dxdy, stored in variable E.  For continuous functions, d^2/dxdy = d^2/dydx, and the program assumes that this is the case.

Formulas Used

For the function f(x,y) given Δx and Δy: 

Let H = Δx and K = Δy.  Note that H and K do not have to be equal values. H and K are typically small (such as 0.0001 to 0.01). 

Then:

d^2/dx^2 = 1/H^2 * ( f(x+H,y) – 2 * f(x,y) + f(x-H,y) )
d^2/dy^2 = 1/K^2 * ( f(x,y+K) – 2 * f(x,y) + f(x,y-K) )
d^2/dxdy = 1/(4*H*K) * ( f(x+H,y+K) – f(x+H,y-K) – f(x-H,y+K) + f(x-H,y-K) )

When prompted for f(x,y), enter the equation in quotes.  For example, enter f(x,y) = x – y^2 as “X-Y^2”.

Program DBLDERIV

Disp "DOUBLE PARTIAL"
Disp "DERIVATIVES"
Input "F(X,Y)=",Y₁
Input "X0=",A
Input "Y0=",B
Input "ΔX=",H
Input "ΔY=",K
A+H→X:B→Y:Y₁→C
A→X:C-2Y₁→C
A-H→X:(C+Y₁)/H²→C
A→X:B+K→Y:Y₁→D
B→Y:D-2Y₁→D
B-K→Y:(D+Y₁)/K²→D
A+H→X:B+K→Y:Y₁→E
B-K→Y:E-Y₁→E
A-H→X:B+K→Y:E-Y₁→E
B-K→Y:(E+Y₁)/(4HK)→E
Disp "C: D/DX²=",C
Disp "D: D/DY²=",D
Disp "E: D/DXDY=",E

Note: 
Pressing [ VARS ], 1, 8 types the ΔX character.
Pressing [ VARS ], 1, 9 types the ΔY character.

Examples

Remember that this program gives approximate values!

Example 1:  f(x,y) = sin(x*y), Radians Mode, x0 = Ï€/2, y0 = Ï€, H = K = 0.001

Results:
d^2/dx^2 = C = 9.62648815    (actual is about 9.62649603)
d^2/dy^2 = D = 2.40662356    (actual is about 2.406624007)
d^2/dxdy = E = 5.033820798   (actual is about 5.03382056)

Example 2:  f(x,y) = x^2*y^2 – x^3,  x0 = 1, y0 = 2, H = K = 0.001

Results:
d^2/dx^2 = C = 2
d^2/dy^2 = D = 2
d^2/dxdy = E = 8
In this case, we get the exact answers. 

Example 3:  f(x,y) = y^2*e^x, x0 = 0.1, y0 = 1.1, H = K = 0.001

Results:
d^2/dx^2 = C = 1.337257  (actual is about 1.337256811)
d^2/dy^2 = D = 2.203418  (actual is about 2.210341836)
d^2/dxdy = E = 2.4313764  (actual is about 2.43137602)

Source:

“Lecture 27: Numerical Differentiation”  This page is from the textbook:  Young, Todd & Mohelnkamp, Martin  “Introduction to Numerical Methods and Matlab Programming for Engineers”  Ohio University Math 3600 Course.   Retrieved January 30, 2016.  Link: https://www.math.ohiou.edu/courses/math3600/lecture27.pdf


Happy computing!

Eddie


 This blog is property of Edward Shore. 2016 


Thursday, February 4, 2016

TI-84 Plus: Simpson’s Rule for f(x,y)

TI-84 Plus: Simpson’s Rule for f(x,y)



If you are familiar with calculus and numerical methods, chances are that you are familiar with Simpson’s Rule.  Normally, Simpson’s Rule applies to functions of one variable, say f(x).  Today, we will apply the Simpson’s Rule to functions of two variables like f(x,y).

Start with the function f(x,y) and your integration limits A, B, C, and D.

Then determine Δx and Δy (labeled E and F in the programs below) by:

Δx = (B – A)/(N – 1)
Δy = (D – C)/(N – 1)

Where N is the number of partitions.  Unlike the Simpson’s Rule for one variable, in this case N must be odd.  Generally, the higher N is, the more accurate the approximation is, with the expense of additional computational time.

Next, build a matrix, let’s say [ I ].   This is your Simpson’s Matrix.  The Simpson’s Matrix is built by the expression

[ I ] = [1, 4, 2, 4, 2, 4, 2, 4, 2, …, 4, 1]^T * [1, 4, 2, 4, 2, 4, 2, 4, 2, …, 4, 1]

The length of the vector used to determine [ I ] depends on N.  A way to build it by the routine:

Store 1 into the element 1 of [ I ]  (first element)
Store 1 into the element N of [ I ]  (last element)
For J from 2 to N – 1
If J is divisible by 2, then store 4 in the jth element of [ I ],
Else store 2 in the jth element of [ I ]

For N = 5, the vector would be built is [1, 4, 2, 4, 1]

And

[ I ] = [1,4,2,4,1]^T * [1,4,2,4,1] =

[[1, 4, 2, 4, 1]
[4, 16, 8, 16, 4]
[2, 8, 4, 8, 2]
[4, 16, 8, 16, 4]
[1, 4, 2, 4, 1]]

Build another matrix [ J ].  The elements are determined by the following formulas:

For row j and column k, the element is f(A + Δx*(j – 1),  C + Δy*(k – 1))

Once finished, multiply every element of [ I ] by [ J ].  This is NOT matrix multiplication.   Then sum all of the elements of the results.  In essence:

S = ∑ (j = 1 to N) ∑ (k = 1 to N)   [I](j,k)* [J](j,k)

Determine the final integral approximation as:

Δx * Δy * 1/9 * S


The Programs

The following are three programs, one that set N = 5, N = 9, and the last one lets the user set a value for N.

Programs:

DBLSIMP:  N = 5

Disp "2D SIMPSONS"
Disp "INTEGRAL"
[[1,4,2,4,1][4,16,8,16,4][2,8,4,8,2][4,16,8,16,4][1,4,2,4,1]]→[I]
Input "F(X,Y)=",Y₁
Disp "X"
Prompt A,B
Disp "Y"
Prompt C,D
.25(B-A)→E
.25(D-C)→F
{5,5}→dim([J])
0→I
For(J,1,5)
For(K,1,5)
A+E(K-1)→X
C+F(J-1)→Y
Y₁→[J](J,K)
I+Y₁*[I](J,K)→I
End
End
I*E*F/9→I
Disp "INTEGRAL=",I



DBLS9:  N = 9 Version

Disp "2D SIMPSONS"
Disp "INTEGRAL"
[[1,4,2,4,2,4,2,4,1][4,16,8,16,8,16,8,16,4][2,8,4,8,4,8,4,8,2][4,16,8,16,8,16,8,16,4][2,8,4,8,4,8,4,8,2][4,16,8,16,8,16,8,16,4][2,8,4,8,4,8,4,8,2][4,16,8,16,8,16,8,16,4][1,4,2,4,2,4,2,4,1]]→[I]
Input "F(X,Y)=",Y₁
Disp "X"
Prompt A,B
Disp "Y"
Prompt C,D
(B-A)/8→E
(D-C)/8→F
{9,9}→dim([J])
0→I
For(J,1,9)
For(K,1,9)
A+E(K-1)→X
C+F(J-1)→Y
Y₁→[J](J,K)
I+Y₁*[I](J,K)→I
End
End
I*E*F/9→I
Disp "INTEGRAL=",I

DBLSMP: N can be set

Disp "2D SIMPSONS"
Disp "INTEGRAL"
Input "F(X,Y)=",Y₁
Disp "X"
Prompt A,B
Disp "Y"
Prompt C,D


Input "N (ODD)=",N
{1,N}→dim([I])
1→[I](1,1)
1→[I](1,N)
For(J,2,N-1)
If fPart(J/2)=0
Then
4→[I](1,J)
Else
2→[I](1,J)
End
End
[I]^T*[I]→[I]

(B-A)/(N-1)→E
(D-C)/(N-1)→F

{N,N}→dim([J])
0→I
For(J,1,N)
For(K,1,N)
A+E(K-1)→X
C+F(J-1)→Y
Y₁→[J](J,K)
I+Y₁*[I](J,K)→I
End
End
I*E*F/9→I
Disp "INTEGRAL=",I


One thing to note, when you are prompted enter your function, do not forget to use quotes.  Example, for x^2*y, enter “X^2*Y”. 

Caution:  Simpson’s Rule is an approximation, 100% accuracy is not guaranteed.

Of the three I recommend DBLSMP so you have control over N. 

Examples and Results

∫ ∫ x^2*y^3 dx dy, A = 0, B = 2, C = 1, D = 5
Result is 416 for both N = 5 and N = 9, agrees with the actual answer

∫ ∫ x^2 + y dx dy, A = 0, B = 2, C = 1, D = 1
Result is 5.666666667 for both N = 5 and N = 9, agrees with the actual answer

∫ ∫ sin(x*y) dx dy, A = 0, B = Ï€, C = 0, D = 0.5
N = 5, result is 0.5568449485
N = 9, result is 0.5568006343
Actual answer is about 0.556797718751

To illustrate that Simpson’s Rule may not work out the best when N is low:
 ∫ x * e^(-y) dx dy,  A = 0, B =5, C = -2, D = 2
N = 5, result is 91.12098525
N = 9, result is 90.70208034
Actual answer is about 90.6715102

Source:
Cooper, Ian.  “Doing Physics With Matlab:  Mathematical Routines”  School of Physics, University of Sydney
Retrieved January 30, 2016

Until next time,
Eddie


This blog is property of Edward Shore.  2016

Monday, February 1, 2016

Video: Decimal/Hexadecimal Conversions

Happy February!

New Video:  Decimal/Hexadecimal Conversions

https://www.youtube.com/watch?v=2b5-Vjcq450

Coming Up

I am going to working on several numerical calculus routines, hopefully I will be posting in the upcoming couple of weeks.  They will be in text (not videos).  

Eddie




HP 15C: The I Squared Rule and HHC 2026

HP 15C: The I Squared Rule and HHC 2026 HHC 2026 Videos Before we start, the videos for HHC 2026 are up! Please take a look, ev...