Showing posts with label fx-CG100. Show all posts
Showing posts with label fx-CG100. Show all posts

Saturday, August 8, 2026

Python: Combination Functions (Micropython)

Python: Combination Functions (Micropython)


This is a set of combination functions. Programmed with a Casio fx-CG 100 but should work on any calculator with Python.


The script is freeware.


Functions included:


 fact(n): Factorial of the non-negative integer n. Some math modules include a factorial function, like Numworks. This function will be used in all the functions on this script.


ncr(n,r): Combination


npr(n,r): Permutation


nhr(n,r): Combination, repetitions are allowed


catalan(n): nth Catalan Number


narayana(n,k): Narayana Number (n, k are positive integers)


binpdf(n,k,p): Binomial Probability:

n: trials

k: number of successes

p: probability of success


bndcdf(n,k,p): Cumulative Binomial Probability – Lower Tail

n: trials

k: number of successes (from 0 to k)

p: probability of success



Script: combo.py

'''

combinatorics 4/12/2026

Edward Shore


round to nearest integer:

int(f+.5)

'''


from math import *


# factorial positive integers

def fact(n):

  f,i=1,1

  while i<=n:

    f*=i

    i+=1

  return int(f+.5)


# a lot of functions will use fact


# combination

def ncr(n,r):

  f=fact(n)/(fact(r)*fact(n-r))

  return int(f+.5)


# permutation

def npr(n,r):

  f=fact(n)/fact(n-r)

  return int(f+.5)


# combination w/repetitions

def nhr(n,r):

  f=fact(n+r-1)/(fact(r)*fact(n-1))

  return int(f+.5)


# catalan numbers

def catalan(n):

  f=fact(2*n)/(fact(n)**2*(n+1))

  return int(f+.5)


# narayana numbers

def narayana(n,k):

  f=1/n*ncr(n,k)*ncr(n,k-1)

  return int(f+.5)


# binomial probability

def binpdf(n,k,p):

  # p=prob

  f=ncr(n,k)*p**k*(1-p)**(n-k)

  return f


# bin lower tail

def bincdf(n,k,p):

  # 0 to k, p=prob

  s=0

  for i in range(k+1):

    s+=binpdf(n,i,p)

  return s



Eddie


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

Casio fx-CG100: The getkey command in Python

Casio fx-CG100: The getkey command in Python


The getkey command: A Demonstration


The fx-CG100 adds a command to its Casioplot module, the beloved getkey command. The getkey returns the key code of the last key pressed from the command’s execution. The getkey allows for custom keyboard, movement of characters, or easy input of menu choices.


The getkey code that is assigned to each key on the fx-CG100 is thankfully fairly simple: it is a combination of the row number and a column number. For instance, the key code 21 is assigned to the [SETTINGS] key while the key code 95 is assigned to the [EXE] key.


Some key codes include:

Left arrow (←)

23

Up arrow (↑)

14

OK key

24

Down arrow (↓)

34

Right arrow (→)

25

EXE

95

1 Key

81

2 Key

82

3 Key

83

4 Key

71

5 Key

72

6 Key

73

7 Key

61

8 Key

62

9 Key

63

0 Key

91


Other key assignments can be found in the manual under the getkey() command in the Casioplot section: https://support.casio.com/global/en/calc/manual/fx-CG100_1AUGRAPH_en/BONDSYlkjdoxxc.html#BONDSYhxeuohnh


Note that the [ ON ] and [ AC ] buttons have no key assigned to them and can not be used with the getkey command.


Example: Calculating Horsepower


The script, hp.py calculates horsepower by one of three sets of inputs:


Press [ 1 ] to input weight (pounds) and time (seconds).

Press [ 2 ] to input weight (pounds) and speed (mi/hr).

Press [ 3 ] to input RPM (revolutions per minute) and torque (foot-pounds).


Using Getkey for Menus

To set up the getkey, I first make a “choice variable”, such as ch, and set it to 0. The while loop contains the menu and does not end until the user presses [ 1 ], [ 2 ], or [ 3 ] (or [ AC ] to terminate the program).   


from casioplot import *


# setup

ch=0


# title screen

while ch==0:

  clear_screen()

  draw_string(0,0,"**HORSEPOWRER CALCULATOR**")

  draw_string(0,20,"**     Inputs Menu     **")

  draw_string(0,40,"1) weight, time",(192,0,0))

  draw_string(0,60,"2) weight, speed",(0,0,192))

  draw_string(0,80,"3) RPM, torque",(0,128,0))

  show_screen()

  k=getkey()

  if k==81:

    ch=1

  elif k==82:

    ch=2

  elif k==83:

    ch=3

  


# calculation

if ch==1:

  w=float(input("weight in lbs:  "))

  t=float(input("time in sec:  "))

  hp=w*(t/5.825)**(-3)

elif ch==2:

  w=float(input("weight in lbs:  "))

  s=float(input("speed in mph:  "))

  hp=w*(s/234)**3

elif ch==3:

  r=float(input("RPM:  "))

  t=float(input("torque (ft-lbs): "))

  hp=(r*t)/5252


clear_screen()

txt="{0:.5f} hp".format(hp)

draw_string(0,0,"Horsepower=")

draw_string(0,20,txt,(192,0,0))

draw_string(0,100,"Press AC to exit.",(0,128,128))

show_screen()


Notes: The getkey is meant to work with the Casioplot commands. Instead of the built-in print command, we use draw_string and show_screen commands to display output. The plus here is that we can have text in color. The show_screen is displayed until either the [ EXE ] or the back buttons is pressed.


The input command returns us to the Shell and is not part of the Casioplot commands.


Source for the Formulas Used: 


Inch Calculator “Engine Horsepower Calculator”. Calc Hub, LLC. 2025. https://www.inchcalculator.com/engine-horsepower-calculator/ Retrieved May 5, 2025.


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.


Classic RPN Stack Operations

  Classic RPN Stack Operations Starting Stack: T:  d Z:  c Y:  b X:  a OVER :   Copies b to the Z and X stacks, while leaving ...