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.


Saturday, August 1, 2026

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues




The programs are listed for the Swiss Micros DM42 and Hewlett Packard 42S.



All examples are on rounded to four decimal places and complex mode turned on (CPXRES).



Monic Quadratic Equation: quad2.raw



The program QUAD2 solves the equation for w:

w² + Y * w + X = 0

Y: contents of stack Y

X: contents of stack X



Code:

00 { 35-Byte Prgm }

01▸LBL "QUAD2"

02 X<>Y

03 ENTER

04 2

05 ÷

06 +/-

07 ENTER

08 X↑2

09 X<>Y

10 R↓

11 X<>Y

12 -

13 SQRT

14 R↑

15 X<>Y

16 ENTER

17 ENTER

18 R↑

19 ENTER

20 R↓

21 X<>Y

22 +

23 R↓

24 -

25 R↑

26 RTN

27 .END.



Example 1: w² + 7 * w + 5 = 0

Stack: Y: 7, X: 5

Results: -6.1926, -0.8074



Example 2: w² – 4 = 0

Stack: Y: 0, X: -4

Results: -2.0000, 2.0000



Characteristic Polynomial: char2.raw



The program CHAR2 find the coefficients of 2 x 2 matrix:

[ [ A, B ] [ C , D ] ]

Result: λ² – T1 * λ + T2 = 0 where

T1 = A + D

T2 = det([[ A, B ] [ C, D ]]) = A * D – C * B



The program provides prompts and results in messages.



Code:

00 { 81-Byte Prgm }

01▸LBL "CHAR2"

02 "[[A,B][C,D]]"

03 AVIEW

04 PSE

05 PSE

06 "L↑2-T1×L+T2=0"

07 AVIEW

08 PSE

09 PSE

10 INPUT "A"

11 ENTER

12 ENTER

13 INPUT "D"

14 ENTER

15 R↓

16 +

17 "T1= "

18 ARCL ST X

19 AVIEW

20 STOP

21 R↓

22 ×

23 INPUT "C"

24 INPUT "B"

25 ×

26 -

27 "T2= "

28 ARCL ST X

29 AVIEW

30 RTN

31 .END.



Example 1: [ [ -6, 4 ] [ 2, 3 ] ]

Results: T1: 3, T2: -26



Example 2: [ [ 5, 8 ] [ -1, 2 ] ]

Results: T1: 7, T2: 18



Eigenvalues of 2 x 2 Matrices: egn2.raw



The program EGNV2 calculates the eigenvalues of a 2 x 2 matrix:

[ [ A, B ] [ C , D ] ]



The program provides prompts and results in messages. The command CPXRES sets the calculator to accept complex number results.



Code:

00 { 106-Byte Prgm }

01▸LBL "EGNV2"

02 CPXRES

03 "[[A,B][C,D]]"

04 AVIEW

05 PSE

06 PSE

07 INPUT "A"

08 INPUT "B"

09 INPUT "C"

10 INPUT "D"

11 RCL "A"

12 RCL "D"

13 +

14 +/-

15 RCL "A"

16 RCL "D"

17 ×

18 RCL "B"

19 RCL "C"

20 ×

21 -

22 X<>Y

23 ENTER

24 2

25 ÷

26 +/-

27 ENTER

28 X↑2

29 X<>Y

30 R↓

31 X<>Y

32 -

33 SQRT

34 R↑

35 X<>Y

36 ENTER

37 ENTER

38 R↑

39 ENTER

40 R↓

41 X<>Y

42 +

43 "E1="

44 ARCL ST X

45 AVIEW

46 PSE

47 PSE

48 R↓

49 -

50 "E2="

51 ARCL ST X

52 AVIEW

53 PSE

54 PSE

55 R↑

56 RTN

57 .END.



Example 1: [ [ -6, 4 ] [ 2, 3 ] ]

Results: 3.8151, - 6.8151



Example 2: [ [ 5, 8 ] [ -1, 2 ] ]

Results: 3.5 ± 2.37792 i


You can download the three programs here: https://drive.google.com/file/d/1SKcVIhKYHnnuhrvBJxfOmwsV8sRTSj0X/view?usp=sharing





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.

Saturday, July 25, 2026

Earth's Radius by Latitude

Earth's Radius by Latitude




Introduction: Calculating the Earth’s Radius



In quick, general calculations, we assume that the shape of the Earth approximates the shape of sphere, with the radius to generally be approximated as 6371 km. However, the shape of the Earth is closer to an oblate ellipsoid. The Earth is flattened at the poles and bulges the greatest at the Equator.



There are several calculations to estimate the Earth’s radius given the latitude (degrees North or South from the Equator), this blog is working with the WGS 84 formula (see Source):



R = √[ ((a² * cos(φ))² + (b² * sin(φ))²) ÷ ((a * cos(φ))² + (b * sin(φ))²) ]



Which is simplified from:



R = √[ (a^4 + b^4 * tan²(φ)) ÷ (a² + b² * tan²(φ))



where:

φ: latitude (in degrees)

a: semi-major axis: 6378.137 km

b: semi-minor axis: 6356.7523142 km

R: radius of kilometers



HP 15C Code: Earth’s Radius by Latitude

Step; Key; Key Code

001: LBL E; 42, 21, 15

002: DEG; 43, 7

003: →H; 43, 2

004: TAN; 25

005: x²; 43, 11

006: STO 1; 44, 1

007: 6; 6

008: 3; 3

009: 7; 7

010: 8; 8

011: . ; 48

012: 1; 1

013: 3; 3

014: 7; 7

015: STO 2; 44, 2

016: 4; 4

017: 14; y^x

018: 6; 6

019: 3; 3

020: 5; 5

021: 6; 6

022: . ; 48

023: 7; 7

024: 5; 5

025: 2; 2

026: 3; 3

027: 1; 1

028: 4; 4

029: 2; 2

030: STO 3; 44, 3

031: 4; 4

032: y^x; 14

033: RCL 1; 45, 1

034: ×; 20

035: +; 40

036: RCL 2; 45, 2

037: x²; 43, 11

038: RCL 3; 45, 3

039: x²; 43, 11

040: RCL 1; 45, 1

041: ×; 20

042: +; 40

043: ÷; 10

044: √; 11

045: RTN; 43, 32



TI-60 Code: Earth’s Radius by Latitude

Step; Key; Key Code



Set degrees mode before beginning.



00: DMS-DD; 39

01: TAN; 34

02: x²; 96

03: STO; 61

04: 1; 01

05: ( ; 53

06: 6; 06

07: 3; 03

08: 7; 07

09: 8; 08

10: . ; 93

11: 1; 01

12: 3; 03

13: 7; 07

14: STO; 61

15: 2; 02

16: y^x; 45

17: 4; 04

18: +; 85

19: 6; 06

20: 3; 03

21: 5; 05

22: 6; 06

23: . ; 93

24: 7; 07

25: 5; 05

26: 2; 02

27: 3; 03

28: 1; 01

29: 4; 04

30: 2; 02

31: STO; 61

32: 3; 03

33: y^x; 45

34: 4; 04

35: ×; 25

36: RCL; 71

37: 1; 01

38: ) ; 54

39: ÷; 55

40: ( ; 53

41: RCL; 71

42: 2; 02

43: x²; 96

44: +; 85

45: RCL; 71

46: 3; 03

47: x²; 96

48: ×; 65

49: RCL; 71

50: 1; 01

51: ) ; 54

52: = ; 95

53: √; 86

54: R/S; 13

55: RST; 22



Examples



Latitude: 80° 00’; Radius (km): 6357.402412

Latitude: 57° 24’; Radius (km): 6362.996788

Latitude: 43° 40’; Radius (km): 6367.986902

Latitude: 9° 15’; Radius (km): 6377.588959

Latitude: 2° 56’; Radius (km): 6378.081467


Source


Planetcalc. “Earth Radius by Latitude (WGS 84)” Timur. 2021. https://planetcalc.com/7721/ Retrieved March 22, 2026.



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.

Saturday, July 18, 2026

TI-60 and HP 65: Distance by Stadia Tacheometry

TI-60 and HP 65: Distance by Stadia Tacheometry




Introduction


The stadia calculation measures the distance from the level or theodolite (measuring device) to a graduated staff. Two readings are taken from the graduated staff, an upper reading (UR) and a lower reading (LR). The word tacheometry means "swift calculation". The distance between the theodolite and the staff is calculated as:


dist = k * s

k = is a factor taken of a radius of focal distance over image distance. Typically, k is set to 100, and will be assumed that k = 100 for these programs.

s = the difference between the upper reading and lower reading on the graduated staff. (UL - RL). The readings are assumed to be in meters.

If the theodolite is tilted at angle α, known as the vertical angle, then the distance becomes:

dist = k * s * cos² α


α is usually given in degrees-minutes-seconds and must be converted to decimal degrees.


TI-60 Program: Stadia Tracheotomy


Store before running: R1: UR (m), R2: LR (m), R3: α (D.MMSS)

Code:

00: 1 ; 01

01: 0 ; 00

02: 0 ; 00

03: × ; 65

04: ( ; 53

05: RCL; 71

06: 1 ; 01

07: - ; 75

08: RCL; 71

09: 2 ; 02

10: ) ; 54

11: × ; 65

12: RCL; 71

13: 3 ; 03

14: DMS-DD; 39

15: cos; 33

16: x²; 96

17: = ; 95

18: R/S; 13

19: RST; 22


HP 65 Program: Stadia Tracheotomy


Input Stack:

Z: vertical angle in degrees-minutes-second (V.MS)

Y: upper reading (UR)

X: lower reading (LR)

Code:

23: LBL

14: D

51: -

02: 2

32: f^-1

08: LOG (10^x)

71: ×

35 07: x<>y

32: f^-1

03: →D.MS (→DD)

31: f

05: COS

32: f^-1

09: √ (x²)

71: ×

24: RTN



Examples



Example 1:

V: 1°05' (Z stack, R3)

UR: 2.014 m (Y stack, R1)

LR: 1.668 m (X stack, R2)

Result: distance ≈ 34.5876 m



Example 2 (from HP 35):

V: 4°17' (Z stack, R3)

UR: 3.144 m (Y stack, R1)

LR: 1.761 m (X stack, R2)

Result: distance ≈ 137.5285 m



Example 3 (from HP 35):

V: -7°21' (Z stack, R3)

UR: 2.817 m (Y stack, R1)

LR: 0.731 m (X stack, R2)

Result: distance ≈ 205.1860 m



Sources



CivilFerba "Measure the distance by stadia method" Video posted on YouTube on December 8, 2018. https://www.youtube.com/watch?v=oon5ayl9DYs Retrieved March 22, 2026



HP-35 Surveying. Hewlett Packard. Buchs, Switzerland. February 1973. pp. 19-20



fx-FD10 Pro. User's Guide. Casio. Tokyo, Japan. 2014. pg. α-16 (alpha-16)



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.



Saturday, July 11, 2026

HP 20S: The July 2026 Program Collection

HP 20S: The July 2026 Program Collection


Triangulation






d = l * sin(α) * sin(ß) ÷ sin(α + ß)


Store in the following registers before calculation:

R1 = measure of angle A

R2 = measure of angle B

R3 = length l from point A to B


Solve:

R4: distance


Code:

01: LBL A; 61, 41 ,A

02: DEG; 61, 23

03: RCL 1; 22, 1

04: SIN; 23

05: ×; 55

06: RCL 2; 22, 2

07: SIN; 23

08: ÷; 45

09: (; 33

10: RCL 1; 22, 1

11: +; 75

12: RCL 2; 22, 2

13: ); 34

14: SIN; 23

15: ×; 55

16: RCL 3; 22, 3

17: =; 74

18: STO 4; 21, 4

19: RTN; 61, 26


Examples


Example 1:

Inputs: R1 = 60°, R2 = 50°, R3 = 10

Output: R4: 7.05990377592


Example 2:

Input: R1 = 30°, R2 = 80°, R3 = 27.5

Output: R4: 14.4101446626


Source:

“Triangulation (surveying)” Wikipedia. https://en.wikipedia.org/wiki/Triangulation_(surveying) (last edited October 24, 2025). Retried March 17, 2026


Payment: Continuous Compounding


PMT = PV * (e^r – 1) ÷ (1 – e^(-r * t))


Store in the following registers before calculation:

R1 = t: number of payments

R2 = r: periodic interest rate (as a decimal)

R3 = PV: present value


Solve:

R4: PMT: present


Code:

01: GTO B; 61, 41, b

02: RCL 3; 22, 3

03: ×; 55

04: (; 33

05: RCL 2; 22, 2

06: e^x; 12

07: -; 65

08: 1; 1

09: ); 34

10: ÷; 45

11: (; 33

12: 1; 1

13: -; 65

14: (; 33

15: RCL 1; 22, 1

16: ×; 55

17: RCL 2; 22, 2

18: ); 34

19: +/-; 32

20: e^x; 12

21: ); 34

22: =; 74

23: STO 4; 21, 4

24: RTN; 61, 26


Examples


Example 1:

Input: t: 36, r: 0.10 ÷ 12, PV: 5,000.00

Output: PMT: 161.434037378


Example 2:

Input: t: 60, r: 0.05 ÷ 12; PV: 26,349.56

Output: PMT: 497.374636585


Electrical Engineering: System Temperature to Noise Figure


When an amplifier is activated, two ways to express noise are the noise temperature (in Kelvin) and the noise figure (in decibels, dB). Using a reference temperature of 290 K, when the noise temperature (T) is known, the noise figure (F) can be calculated by:


F = 10 * log((T + 290) ÷ 290)


If temperature is given in degrees Celsius (°C), then the formula for noise figure becomes:


F = 10 * log((T°C + 563.15) ÷ 290)


since T = T°C + 273.15.


The following program assumes that temperature is given in degrees Celsius.


Store in the following registers before calculation:

R1 = T°C; temperature in degrees Celsius


Solve:

R2: F: noise figure


Code:

01: LBL C; 61, 41, C

02: RCL 1; 21, 1

03: +; 75

04: 5; 5

05: 6; 6

06: 3; 3

07 . ; 73

08: 1; 1

09: 5; 5

10: =; 74

11: ÷; 45

12: 2; 2

13: 9; 9

14: 0; 0

15: =; 74

16: LOG; 51, 13

17: ×; 55

18: 1; 1

19: 0; 0

20: =; 74

21: STO 2; 21, 2

22: RTN; 61, 26


Examples


Example 1:

Input: T = -160 °C (store in R1)

Output: F: 1.43068666235 dB


Example 2:

Input: T = 15°C

Output: F: 2.99642532081 dB


Source:

Ball, John A. Algorithms for RPN Calculators John Wiley & Sons: New York. 1978. ISBN 0-47-03070-8. pp. 266-267


Moderate Exercise: Target Heart Rate Range


The following equations calculate the target heart range for a typical person engaging in moderate exercise. According to the particle by Jenna Fletcher (see Source), the American Heart Society (AHA) states the person is engaged in moderate exercise when their heart rate is 50% to 70% of their maximum heart rate. The maximum heart rate is 220 minus the person’s age.


Disclaimer: This is not medical advice, any questions should be discussed with a doctor or health professional.


The moderate range is determined by:

high = (220 – age) * 0.7 = low * 1.4

low = (220 – age) * 0.5 = high * 5/7


For high intensity, use the range 70% to 85%.


Code:

01: LBL D; 61, 41, d

02: +/-; 32

03: +; 75

04: 2; 2

05: 2; 2

06: 0; 0

07: =; 74

08: ÷; 2

09: 2; 2

10: =; 74

11: R/S; 26

12: ×; 55

13: 1; 1

14: . ; 73

15: 4 ; 4

16: =; 74

17: RTN; 61, 26


Examples


Example 1:

Input: Age 49 (my age at the time of this blog)

Output: low: 85.5, high: 119.7


Example 2:

Input: Age 25

Output: low: 97.5, high: 136.5


Source:

Fletcher, Jenna. “What a target heart rate is and how to calculate it”. Medically Reviewed by Debra Sullivan Ph. D. Medical News Today. January 22, 2024. https://www.medicalnewstoday.com/articles/target-heart-rate-calculator March 16, 2026.



Percentile in a Range


The program calculates the percentile of x in the range [a, b].


percentile = (x – a) ÷ (b – a) * 100%


Store before calculating:

R1 = a, R2 = b, R3 = x


Code:

01: LBL E; 61, 42, E

02: ( ; 33

03: RCL 3; 22, 3

04: - ; 65

05 RCL 1; 22, 1

06: ) ; 34

07: ÷; 45

08: ( ; 33

09: RCL 2; 22, 2

10: - ; 65

11: RCL 1; 22, 1

12: ) ; 34

13: ×; 55

14: 2; 2

15: 10^x; 51, 12

16: =; 74

17: RTN; 61, 26


Examples


Example 1:

Input: R1 = 10, R2 = 50, R3 = 30 (30 in [10, 50])

Output: 50 (50%)


Example 2:

Input: R1 = 85.5, R2 = 117.5, R3 = 110 (110 in [85.5, 117.5])

Output: 76.5625 (76.5625%)


Programming all five of these programs will fill the program space of the HP 20S entirely (99 steps)!


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.

Python: Combination Functions (Micropython)

Python: Combination Functions (Micropython) This is a set of combination functions. Programmed with a Casio fx-CG 100 but should ...