Saturday, December 6, 2025

Basic vs. Python: Numeric Guessing Games (Featuring Casio fx-702P and fx-CG100)

Basic vs. Python: Numeric Guessing Games


Calculators Used


Basic: Casio fx-702P

Python: Casio fx-CG100


Task


Generate two simple number guessing games.


Guess the Number


This is the classic guess the number. The game generates a number (positive integer) at random in a given range. The player guess the number and if it doesn’t match the target number, the player is told whether the target number is lower or higher. The objective is to find the target number in the lowest number of turns.


The pricing game, The Clock Game, from the legendary game show The Price Is Right uses the Guess the Number game for two prizes. The Clock Game, the contestant needs to get the three-digit price correct for each prize within a total of 30 seconds, with the host (Drew Carey or the late Bob Barker) informing the contest whether the correct price is higher or lower.


The code is for a game where the target integer is between 10 and 99.


BASIC: Casio fx-702P


10 PRT "GUESS THE NUMBER"

20 T=INT (RAN#*90+10)

30 C=0

40 G=0


100 INP "GUESS (10-99)",G

110 IF G<10 THEN 100

115 IF G>99 THEN 100

120 C=C+1

130 IF G<T;PRT "HIGHER"

140 IF G>T;PRT "LOWER"

150 IF G=T THEN 200

160 GOTO 100


200 PRT "CORRECT! THE # IS ";T

210 PRT "# GUESSES: ";C


PYTHON: Casio fx-CG100

Script: numguess.py


from random import *


print("Guess the number ")

t=int(random()*90+10)

c=0

g=0


# != means not

while t!=g:

  g=int(input("Guess (10-99)? "))

  c+=1

  if g<t:

    print("HIGHER")

  if g>t:

    print("LOWER")


# exact guess leaves the loop


print("CORRECT! The # is "+str(t)+".")

print("# of guesses: "+str(c))


The major difference between the two programs is that the Basic version uses If statements and Goto line statements, while Python code uses a while loop.


Find the Coin


This is a guessing game where the player is tasked to find a coin in a 10 by 10 grid. The rows and columns are labeled 0 through 9.





BASIC: Casio fx-702P


10 PRT "FIND THE COIN ($)"

30 A=INT (RAN#*10)

40 B=INT (RAN#*10)

50 C=0

60 PRT "GRID 0-9,0-9"


70 INP "X (0-9)",X

80 INP "Y (0-9)",Y

90 R=ABS (A-X)

100 S=ABS (Y-B)

105 C=C+1

110 IF R=S THEN 200

120 PRT S;" ROW";R;" COL"

150 GOTO 70


200 PRT "YOU FOUND IT!"

210 PRT "SCORE= ";C


PYTHON: Casio fx-CG100

Script: findcoin.py


# find the coin, 10 x 10 grid


from random import *

print("FIND THE COIN")

# random integer from 0 to 9

a=randint(0,9)

b=randint(0,9)

c=0

print("GRID 0-9,0-9")


# set up 

r=-1

s=-2


while r!=s:

  x=int(input("X 0-9: "))

  y=int(input("Y 0-9: "))

  r=abs(a-x)

  s=abs(b-y)

  c+=1

  if r==s:

    break

  print(str(s)+" rows "+str(r)+" col")


print("You found it!")

print("SCORE= ",str(c))


Note: Both numguess.py and findcoin.py use the random module, hence it can be adopted on every calculator with a random module. The HP Prime’s random module is urandom.


Have fun, and modify as you like,


Eddie


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


The author does not use AI engines and never will.

Saturday, November 29, 2025

fx-991 CW: Gamma Function

fx-991 CW: Gamma Function



All screenshots were made with Casio’s classpad.net website.



No Gamma Function? No Problem!


The gamma function is used a lot in advanced mathematics. The gamma functions with a lot of definition functions, but the most common one is for values t>0 (in particular Re(t)>0):


Γ(t) = ∫( x^(t-1) × e^(-x) dx, 0, ∞)


This integral is an improper integral and unless we have a calculator that handles infinite limit, we need to use the following:


Γ(t) =


lim ∫( x^(t-1) × e^(-x) dx, 0, w)

w → ∞


Calculators with the integral function can use the above for estimating the gamma function.



Use Basic Properties for Shortcuts


If t is a positive integer, we can use the factorial function:


Γ(t) = (t – 1)!


Example: Γ(16) = (16 – 1)! = 15! ≈ 1.31 × 10^12



If t is in the form n/2 where n is odd (i.e. 1/2 = 0.5, 3/2 = 1.5, 5/2 = 2.5, 7/2 = 3.5, etc.). we can use the product..


Γ(n / 2) = (n – 2) / 2 × (n – 4) / 2 × (n – 6) / 2 × … × 1 / 2 × √π


Example: Γ(3.5) = Γ(7 / 2) = 5 / 2 × 3 / 2 × 1 / 2 × √π = 15 / 8 × √π ≈ 3.32335097


The following pictures demonstrate the use of the above equivalency along with integral estimate:



I hope you find this helpful,


Eddie


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


The author does not use AI engines and never will.

Saturday, November 22, 2025

Numworks Python: Days Between Days Without Converting to Julian Dates

Numworks Python: Days Between Days Without Converting to Julian Dates



Introduction


The script daysbtwn.py find the number days between two days given the month and year. The program is designed to find the dates, not including the last date (to match most financial calculators and spreadsheets), within one calendar year. The program will ask if February 29 is included.



Numworks Python Code: daysbtwn.py


from math import *


# days between dates within a year

# no conversion to julian approach


# store basic calendar

# 0th is 0 to match the months

cal=[0,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31]


# ask for dates

m1=int(input("Month 1? "))

d1=int(input("Day 1? "))

m2=int(input("Month 2? "))

d2=int(input("Day 2? "))


# leap year question

l=0

l=int(input("Include Feb. 29?\nYES=1, NO=0: "))


# sum

if m1==m2:

  s=d2-d1

  if s<=0:

    s+=365+l  

else:

  if m2<m1:

    m2+=12

  s=cal[m1]-d1

  s+=sum(cal[m1+1:m2])

  s+=d2+l


print("DAYS = ",s)


The code has the math module, which can be used with any calculator with Python or the computer version.



Examples


March 14 – March 13 (of next year)

Month 1? 3, Day 1? 14

Month 2? 3, Day 2? 13

Include Feb. 29? 0

Number of Days: 364


March 14 – March 14 (of next year)

Month 1? 3, Day 1? 14

Month 2? 3, Day 2? 14

Include Feb. 29? 0

Number of Days: 365


March 14 – March 15 (of the same year)

Month 1? 3, Day 1? 14

Month 2? 3, Day 2? 15

Include Feb. 29? 0

Number of Days: 1


January 1 – December 31 (of the same year – leap year)

Month 1? 1, Day 1? 1

Month 2? 12, Day 2? 31

Include Feb. 29? 1

Number of Days: 365


In the U.S., wishing you a safe, sane, and Happy Thanksgiving.


Eddie


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

Basic (TI-81) vs. Python (TI-84 Plus CE Python): First and Second Derivative and Integral by Simpson’s Rule

Basic vs. Python: First and Second Derivative and Integral by Simpson’s Rule



Calculators Used


Basic: TI-81

Python: TI-84 Plus CE Python



Task


Estimate the numerical first derivative by the Nearby Secant Line Method:


f'(x) = lim (f(x + h) - f(x - h)) / (2 * h) as h → 0



Estimate the numerical second derivative by:


f''(x) = lim (f(x + h) - 2 * f(x) + f(x - h)) / h^2 as h → 0



Estimate the definite integral by Simpson’s rule:


∫( Y1 dx, x = a to x = b) ≈

(b - a)/(3 * n) + (f(a) + 4 * f1 + 2 * f2 + 4 * f3 + 2 * f4 + ... + 2 * f_n-2 + 4 * f_n-1 + f(b))


n must be an even integer.

fi = f(a + i * (b – a) / n)


BASIC: TI-81


First let’s look at the TI-Basic version with the classic TI-81:


First Order Derivative  (Nearby Secant Line Method)


f'(x) ≈ (f(x + h) - f(x - h)) / (2 * h)


DER1  (61 bytes)

Function

Rad

Disp "D/DX Y1"

1E-4 -> H

Disp "X?"

Input A

A+H -> X

Y1 -> D

A-H -> X

(D-Y1)/(2H)->D

Disp D



Second Order Derivative


f''(x) ≈ (f(x + h) - 2 * f(x) + f(x - h)) / h^2


DER2  (75 bytes)

Function

Rad

Disp "D²/DX² Y1"

1E-4 -> H

Disp "X?"

Input A

A+H -> X

Y1 -> D

A -> X

D-2*Y1 -> D

A-H -> X

(D+Y1)/(H^2) -> D

Disp D


INTGY1  (134 bytes)

Function 

Rad

Disp "SIMPSONS RULE"

Disp "A,B,N?"

Input A

Input B

Input N

A -> X

Y1 -> S

B -> X

S+Y1 -> S

1 -> I

Lbl 1

A+I*(B-A)/N -> X

S+2*Y1 -> S

If Fpart (I/2)≠0

S+2*Y1 -> S

IS>(I,N-1)

Goto 1

S*(B-A)/(3*N) -> S

Disp S


Examples


f(x) = 3*x^3 + 4*x – 1


Y1 = 3 * X³ + 4 * X – 1

f’(2.4): DER1 returns: 55.84

f’’(2.4): DER2 returns: 43.2


∫( 3 * X³ + 4 * X – 1 dX, 0.5, 1.5):

INTGY1 returns with N = 40 returns 6.75


Notes for BASIC


Before running the programs, store the function to the variable Y1. Calling Y1 evaluates the function of the value stored in variable X. Evaluate Y1 at X: [2nd], (Y-VARS), 1


The TI-81 MUST be in Function mode to evaluate Y1, unless an error occurs. The programs set the angle mode to Radians. The value of h is set to a "default" value of 10^-4. We want h to be small, but not too small. A value of h too small can run the risk of floating point errors.


In the integral program:


* I/2 - Ipart (I/2) is Fpart(I/2), but the long way. Python does not have a built-in fractional part function.


* IS>(I,N-1) translates to:


I=I+1

If I>N-1

Goto 1


IS> is the increment (by 1) and skip, a command in TI-Calculator Basic.


In general BASIC languages, evaluation Y1 may take a different approach, depending on the calculator, pocket computer, or computer:


General Basic:


### ...

### X = g(A)

### GOSUB ***

### ...


*** Y = <evaluate f(X)>

*** RETURN


Another approach on a calculator: Use another program


Prog <main>

...

g(A) -> X

Prog "FX" (see below, answer is stored in Y)

...


Prog "FX" (or any name of your choosing)

<evaulate f(X)> -> Y

<end program, an implied Return is executed here>



PYTHON: TI-84 Plus CE Python Edition


The code presented here uses the math module and built-in functions, and should work with any calculator with Python.


DERINTG.py


from math import *

# derivatives and integrals

# 2025-06-01


print("MATH MODULE IS IMPORTED")


# first derivative function

def der1(fx,x,h=1E-3):

  # fx is a string

  # h is optional (def 1E-3)

  # set up lambda

  f=eval("lambda x:"+fx)

  d=(f(x+h)-f(x-h))/(2*h)

  return d


# second derivative function

def der2(fx,x,h=1E-3):

  # fx is a string

  # h is optional (def 1E-3)

  # set up lambda

  f=eval("lambda x:"+fx)

  d=(f(x+h)-2*f(x)+f(x-h))/(h**2)

  return d


# integrals by simpsons rule

def intg(fx,a,b,n=64):

  # fx is a string

  # n is even, optional (def 64)

  # set up lambda

  f=eval("lambda x:"+fx)

  s=f(a)+f(b)

  for i in range(1,n):

    w=f(a+i*(b-a)/n)

    s+=(2*w) if (i/2-int(i/2)==0) else (4*w)

  s*=(b-a)/(3*n)

  return s


I decided to make a script of three functions.


Python uses binary arithmetic, so be prepared to possibly see answers such as 2.999999999999 and 5.00000000000001. The code leaves the answer in floating point form. Feel free to use the round function and/or format specifiers as you see fit.


f(x) = 3*x^3 + 4*x – 1


In Python:

f(x) = 3*x**3+4*x-1


Examples


Store the string to s:

s=”3*x**3+4*x-1”

der1(s,2.4) returns 55.84000299999303

der2(s,2.4) returns 43.19999999324864

intg(s,0.5,1.5) returns 6.75



I hope you find this useful. Enjoy,


Eddie


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

RPN: Certain Integrals to Positive Infinity

RPN: Certain Integrals to Positive Infinity


Introduction


Today’s RPN session deals with improper integrals where the upper limit is positive infinity (∞).


∫( f(x) dx, a, ∞) → lim ∫( f(x) dx, a, t) as t → ∞


Some properties that will be use:

lim 1/t^n as t → ∞ approaches 0

lim e^(-t) as t → ∞ approaches 0

lim p(t) ÷ q(t) as t → ∞ approaches 0 where p(t) and q(t) are polynomials and

degree p(t) < degree q(t)

lim p(t) ÷ q(t) as t → ∞ approaches p_n ÷ q_n where p(t) and q(t) are polynomials and

degree p(t) = degree q(t). p_n and q_n are the leading coefficients of p(t) and q(t), respectively.


All integrals presented will have closed formulas because they have very friendly anti-derivatives.


( 1 ÷ (x^n) dx, a, ∞)



∫( 1 ÷ (x^n) dx, a, ∞) = 1 ÷ ((n - 1) * a^(n - 1))


(Abbreviated) Derivation:

∫( 1 ÷ (x^n) dx, a, ∞)

= lim (1 ÷ x^(n - 1) * -1 ÷ (n – 1) as x → ∞) - (1 ÷ a^(n – 1) * -1 ÷ (n – 1))

= ( 0 * -1 ÷ (n – 1) ) + 1 ÷ ((n – 1) * a^(n – 1))

= 1 ÷ ((n – 1) * a^(n – 1))


∫( 1 ÷ (x^n) dx, a, ∞) = 1 ÷ ((n - 1) * a^(n - 1))


HP 15C Code:

[42, 21, 11]: LBL A

[         1]: 1

[        30]: -

[    43, 36]: LSTx

[        20]: ×

[        15]: 1/x

[    43, 32]: RTN


Stack:

Y: a

X: n


Examples:

a = 2.75, n = 2: 4/11 ≈ 0.36364

a = 4.9, n = 3: ≈ 0.02082


( 1 ÷ ((x - r)*(x - s)) dx, a, ∞)


∫( 1 ÷ ((x - r)*(x - s)) dx, a, ∞)

= 1 ÷ (r - s) * ln( abs((a - s) ÷ (a – r)) )

For best results, a > max(s, r)


(Abbreviated) Derivation:

∫( 1 ÷ ((x - r)*(x - s)) dx, a, ∞)


Simply by partial fractions:

1 ÷ ((x - r)*(x – s)) = 1 ÷ ((r – s) * (x – r)) – 1 ÷ ((r – s) * (x – s))


Anti-derivative:

∫( 1 ÷ ((x - r)*(x - s)) dx)

= 1 ÷ (r – s) * ( ln(abs(x – r)) – ln(abs(x – s)) )

= 1 ÷ (r – s) * ln ( abs((x – r) ÷ (x – s)) )


Limit as x → ∞:

1 ÷ (r – s) * ln ( abs((x – r) ÷ (x – s)) )

= 1 ÷ (r – s) * ln ( abs((1 – r ÷ x) ÷ (1 – s ÷ x)) )

= 1 ÷ (r – s) * ln ( abs(1) )

= 1 ÷ (r – s) * ln(1)

= 0


When x = a

1 ÷ (r – s) * ln ( abs((a – r) ÷ (a – s)) )

= 1 ÷ (r – s) * ln ( abs(1 ÷ [(a – r) * (a -s)]) )

= 1 ÷ (r – s) * ln ( 1 ÷ [abs((a – s) ÷ (a – r))] )

= -1 ÷ (r – s) * ln ( abs((a – s) ÷ (a – r)) )


Then:

∫( 1 ÷ ((x - r)*(x - s)) dx, a, ∞) = 0 - (-1 ÷ (r – s) * ln ( abs((a – s) ÷ (a – r)) ))

= 1 ÷ (r - s) * ln ( abs((a - s) ÷ (a – r)) )


Code:

[42, 21, 12]: LBL B

[    44,  1]: STO 1

[        33]: R↓

[    44,  2]: STO 2

[        33]: R↓

[    44,  3]: STO 3

[45, 30,  1]: RCL- 1

[    45,  3]: RCL 3

[45, 30,  2]: RCL- 2

[        10]: ÷

[    43, 16]: ABS

[    43, 12]: LN

[    45,  2]: RCL 2

[45, 30,  1]: RCL- 1

[        15]: 1/x

[        20]: ×

[    43, 32]: RTN


Stack:

Z: a

Y: r

X: s


Examples:

a = 7.25, b = -3, s = 6: (ln 41 - ln 5) ÷ 9 ≈ 0.23379

a = 11, b = 4, s = 9: (ln 7 - ln 2) ÷ 5 ≈ 0.25055


( 1 ÷ e^x dx, a, ∞)


∫( 1 ÷ e^x dx, a, ∞) = 1 ÷ e^a


Code:

[42, 21, 13]: LBL C

[        12]: e^x

[        15]: 1/x

[    43, 32]: RTN


Stack:

X: a


Examples:

a = 4: e^(-4) ≈ 0.01832

a = 6: e^(-6) ≈ 0.00248


( x ÷ e^x dx, a, ∞)


∫( x ÷ e^x dx, a, ∞) = (a + 1) ÷ e^a


Code:

[42, 21, 14]: LBL D

[        12]: e^x

[    43, 36]: LSTx

[         1]: 1

[        40]: +

[        34]: x<>y

[        10]: ÷

[    43, 32]: RTN


Stack:

X: a


Examples:

a = 4: 5 ÷ e^(-4) ≈ 0.09158

a = 6: 7 ÷ e^(-6) ≈ 0.01735


Eddie


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


The author does not use AI engines and never will.


Basic vs. Python: Numeric Guessing Games (Featuring Casio fx-702P and fx-CG100)

Basic vs. Python: Numeric Guessing Games Calculators Used Basic: Casio fx-702P Python: Casio fx-CG100 Task Generate two simpl...