Sunday, January 3, 2016

TI-84 Plus and HP Prime: Card Sharks Odds Program

TI-84 Plus and HP Prime:  Card Sharks Odds Program

HAPPY NEW YEAR! 

Cards and Calculations.   By the way, is the next card higher or lower than the 9?


The game show Card Sharks may have long been off-air except for reruns on GSN or BUZZR (or even finding videos on YouTube), the game show is still popular.  It is a simple game involving a standard deck of 52 cards.  The object is to predict whether the next card is higher in rank or lower in rank than the preceding card.  In Card Sharks, aces always count as high.  Along with aces, twos are very desirable, while eights are the worst cards in the deck.

Quick Synopsis*:  Each player works on his/her own deck of cards.  The first two games have 5-card hands while the third, if needed has only 3.  A player got control if they won (usually) a survey-type toss up question.  A game is won when a hand is completed or wins the sudden-death question. Winning 2 games won the match for the champion.  The champion got to play Money Cards, where a seven card hand was dealt in two rows of 3 and one final card.  The player then placed bets on whether the card was higher or lower, at even odds.  The last bet had to at least half of the player’s bankroll.

* This was the general game play, for all of the details of the incarnations of game show, please click here:  http://gameshows.wikia.com/wiki/Card_Sharks  (Game Shows Wiki).

The program CSODDS tracks the odds of whether the next card is higher or lower.  CSODDS assumes a 52 card deck (no jokers).   All entries are numerical, hence:

Jacks = 11
Queens = 12
Kings = 13
Aces = 14

You are asked if you want to continue after each card.  The program accounts for the number of cards in each deck.  Have fun!

TI-84 Plus Program CSODDS




seq(4,X,1,14,1)→L₆
0→L₆(1)
While sum(L₆)>0
Disp "J=11 Q=12 K=13 A=14"
Input "CARD:",C
If L₆(C)≠0
Then
L₆(C)-1→L₆(C)
0→L
For(K,2,C-1)
L+L₆(K)→L
End
0→H
For(K,C+1,14)
H+L₆(K)→H
End
Disp "ODDS NEXT CARD IS"
Disp "HIGHER:",H/sum(L₆)
Disp "LOWER:",L/sum(L₆)
Disp "CARDS USED:",52-sum(L₆)
Pause
Else
Disp "NOT AVAILABLE"
End

Menu("CONTINUE?","YES",1,"NO",2)
Lbl 1
End
Lbl 2

HP Prime Program CSODDS



EXPORT CSODDS()
BEGIN
// Card Sharks Odds
// Jan 3 2016 EWS
// Standard deck of 52 cards

LOCAL L6,C,L,H,K,R;
L6:=MAKELIST(4,X,1,14,1);
L6(1):=0;


WHILE ΣLIST(L6)>0 DO
INPUT(C,"Next Card","Card:",
"J=11 Q=12 K=13 A=14");

IF L6(C)≠0 THEN
L6(C):=L6(C)-1;
//L:=ΣLIST(SUB(L6,1,C-1))/
//ΣLIST(L6);
//H:=ΣLIST(SUB(L6,C+1,14))/
//ΣLIST(L6);

L:=0;
FOR K FROM 2 TO C-1 DO
L:=L+L6(K);
END;

H:=0;
FOR K FROM C+1 TO 14 DO
H:=H+L6(K);
END;
L:=L/ΣLIST(L6);
H:=H/ΣLIST(L6);
R:=52-ΣLIST(L6);
PRINT();
PRINT("CARD: "+C);
PRINT("ODDS NEXT CARD IS");
PRINT("HIGHER: "+H);
PRINT("LOWER: "+L);
PRINT("CARDS USED: "+R);
WAIT(0);

ELSE
MSGBOX("CARD N/A");
END;

// Continue?
K:=1;
CHOOSE(K,"Continue?",
{"Yes","No"});
IF K==2 THEN
RETURN;
END;

END;

END;


Sample 10-Card Hand (results rounded to 3 decimal places):

Card
Odds next Card is Higher
Odds Next Card is Lower
Card 1:  K (13)
0.078
0.863
Card 2:  3
0.860
0.080
Card 3:  J (11)
0.224
0.714
Card 4:  9
0.375
0.563
Card 5:  7
0.532
0.404
Card 6:  A (14)
0.000
0.935
Card 7:  6
0.600
0.333
Card 8:  K (13)
0.068
0.886
Card 9:  8
0.442
0.488
Card 10: 4
0.762
0.167

In the near future, I am going to present some programs for the HP 15C.  I tend to emphasize graphing calculators due to (a) availability and (b) programs can be more complex due to the increased memory.  

Eddie



This blog is property of Edward Shore.  2016

Wednesday, December 30, 2015

Pascal’s Triangle: Polynomials and TI-84 Plus

Pascal’s Triangle:  Polynomials and TI-84 Plus

Pascal's Triangle

Powers of 2

Pascal’s Triangle holds a great number of properties.   For instance, the sum of each row (R) is a power of two (2^R).  The top row is referred to as row 0.  Hence:

1 = 2^0
1 + 1 = 2 = 2^1
1 + 2 + 1 = 4 = 2^2
1 + 3 + 3 + 1 = 8 = 2^3
1 + 4 + 6 + 4 + 1 = 16 = 2^4
1 + 5 + 10 + 10 + 5 + 1 = 32 = 2^5
1 + 6 + 15 + 20 + 15 + 6 + 1 = 64 = 2^6
and so on…

Binomial Expansion

Take a look what happens when you expand the binomial (x + y)^R:

(x + y)^0 = 1

(x + y)^1 = 1*x + 1*y

(x + y)^2 = 1*x^2 + 2*x*y + 1*y^2

(x + y)^3 = 1*x^3 + 3*x^2*y + 3*x*y^2 + 1*y^3

(x + y)^4 = 1*x^4 + 4*x^3*y + 6*x^2*y^2 + 4*x*y^3 + 1*y^4

(x + y)^5 = 1*x^5 + 5*x^4*y + 10*x^3*y^2 + 10*x^2*y^3 + 5*x*y^4 + 1*y^5

(x + y)^6 = 1*x^6 + 6*x^5*y + 15*x^4*y^2 + 20*x^3*y^3 + 15*x^2*y^4 + 6*x*y^5 + 1*y^6

and so on…

Notice the coefficients (in blue)?  They represent rows of the Pascal’s Triangle. 

Combinatorics

The formula for find the number of combinations of N items from R is:

COMB(R, N) = R nCr N = R! / ((R – N)! * N!)

If you R stand for a row of the Pascal’s Triangle and N stand for an entry (starting from 0), you can get R nCr N from the triangle. 

For instance, for the 4th row (R = 4 with entries 1, 4, 6, 4, 1), 2nd entry (N = 2, with left most entry designated as 0), Pascal’s Triangle will state that 4 nCr 3 = 4! / (2! * 2!) = 6.

For the 5th Row (R = 5):
N = 0, 5 nCr 0 = 1
N = 1, 5 nCr 1 = 5
N = 2, 5 nCr 2 = 10
N = 3, 5 nCr 3 = 10
N = 4, 5 nCr 4 = 5
N = 5, 5 nCr 5 = 1

Sierpinksi Triangle

One of the books I got for Christmas is the book “The Magic of Math: Solving for x and Figuring Out Why” written by Arthur Benjamin.  The book is well written and if you want a good read I recommend this book.  It has something for everyone. One of things I learned from Benjamin’s book is that if you mark all the odd numbers, and you take many rows, you get the famous fractal the Sierpinski Triangle.

Take a look at the diagram below:

Making the Sierpinski Triangle

TI-84 Plus:  Generating a Row of Pascal’s Triangle

A short program to generate a row of Pascal’s Triangle.  The result is stored in list L6.  The first entry is the 0th entry.

Input "ROW:",R
R+1→dim(L₆)
For(K,0,R)
R nCr K→L₆(K+1)
End
Disp "L₆:"
Pause L₆

I think this is self-explanatory.


Eddie

HAPPY NEW YEAR!

Source:

Benjamin, Arthur.  The Magic of Math: Solving for x and Figuring Our Why.  Basic Books:  New York.  2015


This blog is property of Edward Shore.   2015.

Friday, December 18, 2015

Mathematical Art with the HP Prime and TI-84 CE (December 18, 2015 edition)

Mathematical Art with the HP Prime and TI-84 Plus CE (December 18, 2015 edition)

More visuals for your pleasure.  Happy Holidays and Merry Christmas,

Eddie


TI 84 Plus CE: Linear Box Illusion 

HP Prime:  Confetti 2016

HP Prime: The Random-Colored Diamond

HP Prime:  Cutting the Line

HP Prime: A Tear

HP Prime: Open Wide

TI-84 Plus CE: Child’s Flowers


This blog is property of Edward Shore. 2015.

Next time will probably be after Christmas!   Thank you all for your support!   Eddie

Sunday, December 13, 2015

Mathematical Art with the HP Prime and TI-84 CE (December 13, 2015 edition)

Mathematical Art with the HP Prime and TI-84 Plus CE (December 13, 2015 edition)

Math is not only fun but can also be creative.  If you want to know I did these plots, please leave a comment below.  Enjoy and best always,

Eddie

HP Prime: Linear Waves

TI -84 Plus CE:  The Arms of Sine

TI-84 Plus CE: Icicles

HP Prime:  Fire From the Flask

TI-84 Plus CE:  Pyramid Illusion

TI-84 Plus CE:  Circular Illusion

HP Prime: Folium Roller Coaster

HP Prime:  Mantra Tiles
This was inspired by this morning's visit to Mantra Coffee in Azusa, CA


The last graphic was inspired by a visit to the Mantra Coffee Company in Azusa, CA (http://www.mantracoffeeco.com/).  



This blog is property of Edward Shore.  2015



Thursday, December 10, 2015

HP Prime and Casio Prizm: 3 Point Neville's Method

HP Prime and Casio Prizm: 3 Point Neville's Method

Introduction

Neville’s Method is an interpolation method to find an approximation of f(x) at point x.  The function may or may not be known.  The method uses Lagrange interpolation polynomials.

For this blog entry, we will work with 3 known points to interpolate a fourth point.  We certainly can use a higher order interpolation if desired or needed.  In general for the 3 known points (x0, y0), (x1, y1), and (x2, y2):

P(0,0)



P(0,1)

P(1,1)

P(0,2)

P(1,2)

P(2,2)



Think of this like a collapsing bracket, collect two neighboring points until a column is completed.  Repeat the process until P(0,n) is reached.   In this case of 3 points, the approximate answer is y ≈ P(0,2).

Formulae:

P(0,0) = (x0, y0)
P(1,1) = (x1, y1)
P(2,2) = (x2, y2)

P(0,1) = ((x1 – x)*y0) + (x – x0)*y1) / (x1 – x0)
P(1,2) = ((x2 – x)*y1) + (x – x1)*y2) / (x2 – x1)

P(0,2) = ((x2 – x)*P(0,1) + (x – x0)*P(1,2)) / (x2 – x0)
Y = P(0,2)

The NEVILLE3 program works with the 3-point interpolation where lists presents the points {x0, x1, x2} and {y0, y1, y2}.  If you use a TI-84 Plus, the Casio Prizm version can be adopted (with appropriate changes).

HP Prime Program:  NEVILLE3

Input:  lx and ly and list of three elements, {x0, x1, x2} and {y0, y1, y2}, respectively, and x is the target point.

EXPORT NEVILLE3(lx,ly,x)
BEGIN
// Neville 3-Point Approx.
// EWS 2015-12-07

LOCAL la,a,y,k,n;

la:={0}; // for consistency

FOR k FROM 1 TO 3 DO

IF k==3 THEN
a:=((lx(3)-x)*la(2)+(x-lx(1))*
la(3))/(lx(3)-lx(1));
ELSE

a:=((lx(k+1)-x)*ly(k)+(x-lx(k))*
ly(k+1))/(lx(k+1)-lx(k));
END;
la:=CONCAT(la,{a});
END;
y:=la(4); RETURN y;
END;


Casio Prizm Program NEVILLE3

Input:  List 1 and List 2 and list of three elements, {x0, x1, x2} and {y0, y1, y2}, respectively, and x is the target point.

“3 PT NEVILLE”
“LIST X”? → List 1
“LIST Y”? → List 2
“X?” → X
{0} → List 3
For 1 → K To 3
If K = 3
Then
(( List 1[3] – X ) * List 3[2] + ( X – List 1[1]) * List 3[3] )
÷ (List 1[3] – List 1[1]) → A
Else
(( List 1[K+1] – X ) * List 2[K] + (X – List 1[K]) * List 2[K+1] )
÷ (List 1[K+1] – List 1[K] ) → A
IfEnd
Augment(List 3,{A}) → List 3
Next
List 3[4] → Y
Y


Examples:

Example 1;
List X/List 1 = {16, 64, 100}
List Y/List 2 = {0.25, 0.125, 0.1}
X = 81

Result:  0.1058511078

Example 2:
List X/List 1 = {-2, 0, 2}
List Y/List 2 = {11, 4, 11}
X = 3

Result:  19.75


Sources:

Mitchell, Kevin.  “Neville’s Method”  Simon Fraser University. MACM 316 – Fall 2010.   http://people.math.sfu.ca/~kevmitch/teaching/316-10.09/neville.pdf    Retrieved December 4, 2015

Wikiversity.  “Topic:  Numerical analysis/Neville’s algorithm examples”  https://en.wikiversity.org/wiki/Topic:Numerical_analysis/Neville%27s_algorithm_examples  Retrieved December 4, 2015



This blog is property of Edward Shore.  2015

Friday, December 4, 2015

HP Prime: Slopefield (Differential Equations)

HP Prime:  Slopefield (Differential Equations)

Here is a program that graphs the slopefield of a differential equation in the form dy/dx = f(x,y).  This program is adopted from Alan Ziv’s SLPFIELD program, which Ziv programmed for the Casio fx-9850G.  The link to the original program is here:  http://www.spiderpixel.co.uk/caspro/progs/9x50g/slopefield.txt


For the HP Prime version, enter dy/dx in quotes, and use capital letters X and Y.  I recommend that you are in the Function app for this.  The window should be set up before running SLOPEFIELD.

Program SLOPEFIELD

EXPORT SLOPEFIELD(dxy)
BEGIN
// dxy: dx/dy = f′(x,y)
// Adopted from Alan Ziv -
// Casio
// dxy is in quotes
LOCAL X,Y,C,D,J,K,G,H;
LOCAL F,xa,xb,ya,yb,E;

// Use the Function App
RECT();
C:=(Ymax-Ymin)/220;
D:=(Xmax-Xmin)/320;

FOR J FROM 0 TO 50 DO
E:=Xmin+(8*J+3)*D;
FOR K FROM 0 TO 27 DO
F:=Ymin+(8*K+3)*C;

X:=E;
Y:=F;
G:=EXPR(dxy);
H:=2*D;

IF ABS(G)>C/D THEN
H:=2*C/ABS(G);
END;

IF 2*H>D THEN
xa:=E-H; ya:=G*(xa-E)+F;
xb:=E+H; yb:=G*(xb-E)+F;
LINE(xa,ya,xb,yb);
ELSE
LINE(E,F+2*C,E,F-2*C);
END;
END;
END;

// Draw Axis
LINE(Xmin,0,Xmax,0,#C0C0C0h);
LINE(0,Ymin,0,Ymax,#C0C0C0h);
WAIT(0);

END;

 Examples:

dy/dx = X*^Y^2 + 3*X

 
dy/dx = Y*SIN(X)

This blog is property of Edward Shore.  2015

Eddie


HP Prime: Gauss-Jordan Elimination Method

HP Prime:  Gauss-Jordan Elimination Method

I received an email requesting some programs of various numerical methods.   One of the methods is the Gauss-Jordan Elimination Method. 

Basically, the Gauss-Jordan Elimination Method is a step-by-step method of matrix row operations to reduce a matrix A = [ X | Y ] where X is (mostly) a square component joined by a column vector Y to the form [ I | R ], which I represents an identity matrix portion where the diagonal elements are 1. 

You can quickly execute the method by use of the RREF (Reduced Row Echelon Form) function, which is present on many graphing calculators (if not all of them these days). 

The HP Prime program GAUSSJORDAN is shows a step by step method.  What the program does is:

1. Take the dimensions of the matrix.
2. Starting with column 1, pivot on element (1,1).  This is accomplished by one of two operations.  SCALEADD which multiplies the first row by a factor and adds it to a target row k.  The goal is reduce the element (k,1) to 0, for all k ≠ 1.
3. Once step 2 is completed, the SCALE command is used to divide the value of element (1,1) to reduce it to 1, if necessary.
4. Repeat steps 2 and 3 for each column until the number the rows is reached.

Notes:

1. HP Prime’s SCALEADD is the *Row+ command for the Casio and TI graphing calculators.  Similarly, HP Prime’s SCALE is the *Row command for the Casio and TI graphing calculators. 
2. The program requires that the matrix have all elements that are (1,1), (2,2), (3,3), etc. are non-zero.  Otherwise an error occurs.  You can swap rows by the rowSwap command if necessary to get the matrix in proper form.
3. While the program GAUSSJORDAN shows you step by step, it shows one order of approach, which may or may not be the most efficient number of steps.
4.  If the HP Prime is in Standard mode, you may see 0.9999999999999 or some number to the 10^-13 power.   This is due to the rounding mechanisms.  Feel free to round these numbers to 1 and 0, respectively.

With all this in mind, here is the program: 

Program GAUSSJORDAN

EXPORT GAUSSJORDAN(mat)
BEGIN
// Guass-Jordan Elimination
// 2015-12-04
// Matrix MUST have
// mat[k,k]≠0

LOCAL l,r,c,j,k,h,v;
PRINT();

l:=SIZE(mat);
r:=l[1]; c:=l[2];
j:=1;

// Main row loop
FOR k FROM 1 TO r DO

// Secondary row loop
FOR h FROM 1 TO r DO

// k = target row
// h = test row
// Pivot operation
IF h≠k THEN
v:=−mat[h,k]/mat[k,k];
mat:=SCALEADD(mat,v,k,h);
PRINT();
PRINT("After Step "+j);
PRINT(mat);
WAIT(0);
j:=j+1;
END;

IF mat[k,k]≠1 THEN
v:=1/mat[k,k];
mat:=SCALE(mat,v,k);
PRINT();
PRINT("After Step "+j);
PRINT(mat);
WAIT(0);
j:=j+1;
END;

END;
END;

PRINT();
PRINT("Result:");
PRINT(mat);

// Final WAIT not needed

RETURN mat;

END;


Examples:

M2 = [[1,2,4,1] [3, -3, 3, 0] [6, 6, 8, 5]]
Final Result* (see Note 4) of GAUSSJORDAN(M2):
[[1,0,0,0.53333333333]. [0,1,0,0.43333333333], [0,0,1,-0.1]]

M3 = [[0,4,-1,-1], [2,5,2,2], [3,3,6,3]].
Running GAUSSJORDAN(M3) with M3 as is will get an error.  Why?  See M3[1,1] = 0.  We must make it non-zero.  Do this by swapping rows.
M3≔rowSwap(M3,1,2) to make the matrix [[2,5,2,2], [0,4,-1,-1], [3,3,6,3]].  Now you are ready to go.
GAUSSJORDAN(M3) returns [[1,0,0,2.6], [0,1,0,-0.4], [0,0,1,-0.6]]

Thank you Luis for the email and the suggestions. 


Source:  Cazelais, Gilles. “Gauss-Jordan Elimination Method”  http://pages.pacificcoast.net/~cazelais/251/gauss-jordan.pdf  Retrieved December 4, 2015



Until next time, stay safe everyone because it is a crazy planet we live on. 


Eddie

Sunday, November 29, 2015

HP Prime: Approximate Length of Daylight

I hope you had a great weekend and a great Thanksgiving.  For those of you in the United States, hopefully Friday was peaceful and not crazy.

HP Prime: Approximating the Length of Daylight in Hours

This time, we are taking a slightly more complex formula.   The inputs are:

lat = Earth's latitude of the observer.  (from -90° (South) to 90° (North))

day = the number of days since the December solstice (around December 21-22).  Note that is different from many approximate formulas which used the vernal equinox as a starting point.  

The formula used is a simplification of the Final Formula presented by Herbert Glarner (http://www.gandraxa.com/).  For the complete article and the derivation, please click on link:

http://www.gandraxa.com/length_of_day.xml    (Retrieved November 23, 2015)

The algorithm used in the DAYLIGHT is:

With the inputs lat and day:

m = 1 – tan(lat°) * tan(23.439° * cos(480/487 * day))

If m > 2, let m = 2.   If m < 0, let m = 0.

Then:

b = acos(1 – m)/180 *24

Where b is the length of daylight in hours.

I adjusted the formula to allow for all inputs to be in degrees.  Glamer had mixed inputs for the trigonometric functions.

Here is the program:

Program DAYLIGHT:

EXPORT DAYLIGHT(lat,day)
BEGIN
// latitude, days from
// December solstice
LOCAL m,b,a:=HAngle;
HAngle:=1; // Degrees
m:=1-TAN(lat)*TAN(23.439*COS(
480/487*day));
IF m<0 THEN
m:=0;
END;
IF m>2 THEN
m:=2;
END;
b:=ACOS(1-m)/180*24; // hours
RETURN b;
HAngle:=a;
// www.grandraxa.com
END;

Examples:

Let’s assume a 365 day and the December solstice was December 22. 

Latitude:  -60°, March 1  (day = 69)
Result:  Approx. 16.67808 hours

Latitude: 54°,  July 24  (day = 214)
Result:  Approx. 16.03426 hours

For another approximate formula, I wrote on one for the HP 35S here:  http://edspi31415.blogspot.com/2013/05/hp-35s-approximate-length-of-sunlight.html


Have a great day, 

Eddie


This blog is property of Edward Shore.  2015





Symmetric Quartic Equation - Python

Symmetric Quartic Equation - Python Note: The code is programmed with a TI-84 Evo. No modules are used, so the scripts should be adopt...