Monday, February 29, 2016

Quick Tricks: Casio Programming Calculators and webcal.freetzi.com

Quick Tricks:  Casio Programming Calculators and webcal.freetzi.com

 I spent the last week learning how the Casio fx-3650p can be programmed to execute amazing calculations.  The reason why I say amazing is that on the surface, the fx-3650p is limited, both in programming space (360 bytes allocated on 4 program slots) and the number of variables (7: A, B, C, D, X, Y, M).  Please check out the WebCal Page Counting Machine, run by David Chuek: 


The website is in Chinese (I believe) and I am grateful for Google Translate because I am not proficient in Chinese. 

On this page advanced programs for the fx-3650p including:

* Gamma Function, Elliptic Functions (for x<1), Beta, Zeta, and other special functions
* Determinant of a 3 x 3 Matrix
* Squaring a square matrix
* 4 and 8 point Fast Fourier Transform
* Geometry:  Distance from a point to a line, area from a set of vertices
* Prime Factorization
* Engineering Programs: Snell’s Laws, Prism, Ohm’s law
* Days between Dates, Day of the Week

If you have a Casio programming calculator, check this awesome website out.

While I was learning to code the programs myself, I notice there were several tricks that were used to save space.  I want to test the tricks for the Casio fx-5800p and Casio fx-9860GII (Linear Input Mode).   The case for the fx-9860GII should be the same for other modern Casio graphing calculators (9750g, Prizm). 

Quickly Setting X = 1 and Y = 0

Syntax:  Pol(1,0)
Calculators:   fx-3650p only

Note:  Pol(1,0) sets I to 1 and J to 0 for the fx-5800p.

The Power of Implied Multiplication

Most Casio programming calculators have only single-letter variables, which allows for implied multiplication.  For example, typing AB multiples the values stored in A and B.  In the examples listed below, I use variables A and B, but it applies to all the other variables. 

Implied Multiplication and Square Root

Syntax:  √AB
What it calculates:  √(A*B)
Calculators: fx-3650p, fx-5800p, fx-9860GII

Example:  A = 5, B = 10;  √AB returns 7.071067812  (√50)

Implied Multiplication and Powers

Syntax:  n^AB
What it Calculates:  (n^A)*B
Calculators: fx-3650p, fx-9860GII (linear input only)

Example:  2^AB where A = 5, B = 10;  2^AB returns 320   (2^5*10)

The Jump Function Shortcuts

If the Expression is Nonzero

Syntax:  (var/expression) ⇒ (execute this statement of var/expression ≠  0) : (next command)
The next command must exist or an error occurs

Calculators:  fx-3650p, fx-5800p, fx-9860GII

Example: Ask for A and B.  Divide A by B, unless B is 0. 
Prog2
? → A: ? → B: B ⇒ A ÷ B: Ans

A = 13.5, B = 2.7, result is 5
A = 13.5, B = 0, result is whatever is last stored in Ans

Using the Jump Function to create an AND condition

Connecting two or more jump functions (⇒) to create an AND condition. 

Syntax – 2 Conditions:
(condition 1) ⇒ (condition 2) ⇒ (do if conditions 1 and 2 are true) : (next command)

Syntax – 3 Conditions:
(cond 1) ⇒ (cond 2) ⇒ (cond 3) ⇒ (do if all three conditions are 3) : (next command)
  
Example: Calculate the eccentricity of an ellipse, assuming A ≥ B.  If anything improper is entered, -1 is returned.

Prog3
? → A: ? → B: -1 → C: A ≥ B ⇒ A > 0 ⇒ B > 0 ⇒ √(1 – B2/A2 → C: C

A = 11.5, B = 3.6, result 0.949738796
A = 6, B = 8, result -1
A = -2, B = 1, result -1

Independent Memory

M+:  Calculate the expression, add to M and store the result to M
M-:  Calculate the expression, subtract from M and store the result to M

Syntax:  (expression) M+,  (expression) M-
Calculators: fx-3650p, fx-5800p. 
Note:  This should work on any Casio calculator with M+ and M- available on the keyboard.

Example: 
Store 7 in M.  
3^2 + 4 M+ returns 13 in the display, with M = 20
2^3 – 1 M- returns in the display, with M = 13

Absolute Value

Does you calculator not have an absolute value function?  Try this trick:

Syntax: √A2
What it calculates:  |A|
Calculators:  fx-3650p, fx-5800p, fx-9860GII

Examples:  A = 55, B = -55
√A2 returns 55
√B2 returns 55

Extracting the Integer Part of a Number when the Function Int/Intg is not available

Two ways to do it:
* Syntax:  Fix 0:  Rnd:  Norm 1
* Syntax:  Switch the fx-3650p to Base Mode, 3 for Decimal, then enter your program

The Last Answer (Ans) skips over a prompt

Syntax:  (calculation):  ? → var:  f(Ans)
Calculators: fx-3650p, fx-5800p, fx-9860GII

Example:  Calculate 5^1.5, ask for A, and add it to 5^1.5
Prog1
5^1.5 : ? → A:  Ans + A
Let A = 2, the result is 13.18033989
Let A = 9.75, the result is 20.93033989


Happy Leap Year Day!   Talk to you next time,

Eddie


This blog is property of Edward Shore.  2016


Sunday, February 21, 2016

Scientific Calculator Keyboard Challenge II

Feeling creative?  Ever dreamed of the perfect calculator?  Here is a chance to design a calculator.  It can cover any part of mathematics you want:

*  Scientific
*  Financial
*  Programming/Graphing
*  Algebraic and/or RPN (Reverse Polish Notation)

Or if you want to go a specialized route, like Calculated Industries, consider:

*  Construction
*  Unit Conversion
*  Gaming
*  Geometry Applications
*  Civil Engineering
*  Physics 
*  Astronomy
*  Stock Market
*  Common Every Day Applications

It can be any combination!

Rules:
*  The keyboard must be used, including the arrow keys.  Assume that the calculator will have the necessary screen size.  You have 16 blank keys, of course, you can/should consider using shift and alpha keys.
*  The keyboard has a double-wide Enter key.  RPN and Hewlett Packard fans rejoice!
*  The marked keys must remain.  

Other than that, go wild!  Leave your creation in the comments.  




Have fun!

Eddie

P.S. If you want another design, I had a post where you designed a calculator keyboard using only 25 keys.  The link is here:

http://edspi31415.blogspot.com/2014/05/scientific-calculator-keyboard-challenge.html

This blog is property of Edward Shore - 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