A collection of 20 programs that showcase the elegance of RPN programming on Hewlett-Packard's legendary scientific calculator

There's something deeply satisfying about programming a calculator from 1982.

The HP-15C sits in my hand like a well-worn book—compact, purposeful, its keys worn smooth from decades of use. It has 448 bytes of program memory. Not megabytes. Not kilobytes. Bytes. My text editor uses more memory to display the cursor.

And yet, within those 448 bytes, you can implement the same algorithms that Babylonian scribes carved into clay tablets, that Newton scribbled in the margins of his notebooks, that power the floating-point units of modern CPUs. The constraint isn't limiting—it's liberating. Every keystroke matters. Every register is precious. You learn to think in the language of the machine.

I recently sat down to write a collection of programs for the HP-15C, and what emerged was something like a guided tour through the history of mathematics itself. From Euclid's algorithm (300 BCE) to Robert May's discovery of chaos in the logistic map (1976), these twenty programs span three millennia of mathematical thought—all executable on a device that runs on three button cells.

Why the HP-15C?

When Hewlett-Packard introduced the HP-15C in 1982, it represented the pinnacle of handheld scientific computing. It offered:

  • Complex number arithmetic built into the hardware
  • Matrix operations for up to 8×8 matrices
  • Numerical integration and root-finding via the Solve function
  • Programming with labels, conditionals, loops, and subroutines
  • RPN (Reverse Polish Notation) that eliminates parentheses and mirrors how you actually think about calculations

The calculator was so beloved that when HP discontinued it, prices on the secondary market soared into the hundreds of dollars. HP eventually reissued it in 2011, and today you can run faithful emulators on any smartphone. But nothing quite matches the tactile feedback of those original keys.

The Programs

I've organized twenty programs into four categories, each exploring different corners of computational mathematics:

Number Theory & Fundamentals

The journey begins with the atoms of arithmetic. Prime testing via trial division connects us to every mathematician who has ever wondered whether a number can be factored. Euclid's GCD algorithm—the oldest algorithm still in common use—reduces fractions and underlies modern cryptography. The Fibonacci sequence reveals the golden ratio lurking in rabbit populations and sunflower seeds.

Pascal's Triangle encodes binomial coefficients, answering questions like "how many 5-card poker hands are possible?" (Answer: C(52,5) = 2,598,960). And the humble digital root—repeatedly summing digits until you get a single digit—connects to the medieval practice of "casting out nines" to check arithmetic.

The Collatz Conjecture is here too: that maddening sequence where you halve even numbers and triple-plus-one odd numbers. Does every starting point eventually reach 1? We've checked up to 268, but no one can prove it. Paul Erdős said mathematics may not be ready for such problems. Your HP-15C can trace the journey.

Approximating Constants

π and e are irrational—their decimal expansions never terminate or repeat. But we can approach them through computation.

The Monte Carlo method estimates π by throwing random darts at a square and counting how many land inside an inscribed circle. It's slow (error decreases only as 1/√n), but watching π emerge from pure randomness feels like witnessing order crystallize from chaos.

Wallis's product (1655) builds π from an infinite product of fractions: (2/1)·(2/3)·(4/3)·(4/5)·... It converges glacially, but it proves π emerges from pure arithmetic, no geometry required.

Euler's number e appears through its limit definition: (1 + 1/n)n as n approaches infinity. This is the mathematics of continuous compound interest—nature's growth constant.

Continued fractions reveal why some rational approximations are better than others. When you expand π as [3; 7, 15, 1, 292, ...], that large coefficient 292 explains why 355/113 is such an extraordinarily good approximation—accurate to six decimal places with only three digits in the denominator.

The harmonic series 1 + 1/2 + 1/3 + ... diverges, but so slowly that you need 1043 terms to exceed 100. The partial sums, called harmonic numbers, appear everywhere from quicksort analysis to the coupon collector problem.

Numerical Methods

Here we turn the calculator into a computational engine.

Newton-Raphson iteration finds square roots with quadratic convergence—each iteration roughly doubles the correct digits. The algorithm is identical to what the Babylonians used 3,500 years ago; we just call it Newton's method now.

The numerical derivative uses the central difference formula to estimate slopes. Define any function, point the routine at a value, and it computes the derivative. Calculus without limits.

The quadratic solver exploits the HP-15C's complex number support. When the discriminant goes negative, it doesn't give up—it returns the complex roots. Every quadratic has solutions; sometimes they're just not real.

The matrix determinant demonstration shows off the HP-15C's remarkable linear algebra capabilities. It can invert 8×8 matrices in your hand. In 1982. On three button cells.

Dynamics & Applications

The collection closes with programs that connect mathematics to the real world.

The logistic map xn+1 = r·xn·(1-xn) is a simple population model that produces chaos. For r between 3.57 and 4, the system never settles down—it bounces forever, never repeating, exquisitely sensitive to initial conditions. This is the butterfly effect, executable on your calculator.

Compound interest with regular deposits answers the retirement planning question: what will I have after 30 years? The mathematics is simple; the results are staggering.

Zeller's congruence (1887) computes the day of the week for any date. Enter July 4, 2024, and learn it was a Thursday. The formula elegantly encodes calendar quirks into pure modular arithmetic.

Finally, the gamma function extends factorial to non-integers. Using the HP-15C's built-in numerical integration, you can compute that (1/2)! = √π/2. The factorial function, the circle, and an irrational number—connected through an integral.

How to Enter Programs on the HP-15C

If you're new to HP-15C programming, here's what you need to know.

Understanding the Basics

The HP-15C uses Reverse Polish Notation (RPN). Instead of typing 2 + 3 =, you enter 2 ENTER 3 +. The ENTER key pushes values onto a 4-level stack (X, Y, Z, T registers), and operators consume values from the stack and push results back.

Programs are simply sequences of keystrokes stored in memory. When you run a program, the calculator executes those keystrokes automatically.

Entering a Program

  1. Switch to Program Mode

    Press g P/R (the gold shift key, then P/R). The display will show 000- indicating you're at program step 000 in program mode.

  2. Clear Existing Programs (Optional)

    If you want to start fresh, press f CLEAR PRGM (the orange shift key, then CLEAR PRGM). This erases all stored programs.

  3. Enter the Program

    Simply press the keys as listed in the program. Each keystroke is recorded as a program step. For example, to enter:

    LBL A
    STO 0
    2
    ÷
    RTN

    You would press:

    • f LBL A (creates label A)
    • STO 0 (store in register 0)
    • 2
    • ÷
    • g RTN (return from subroutine)
  4. Special Keys
    • Labels: f LBL followed by A-E, 0-9, or .0-.9
    • Conditionals: g x=0?, g x≤y?, etc. skip the next instruction if false
    • Jumps: GTO followed by a label
    • Subroutine calls: GSB followed by a label
    • Return: g RTN
  5. Exit Program Mode

    Press g P/R again to return to Run mode.

Viewing and Editing Programs

  • Single-step through: In program mode, press SST (single step) to move forward through program lines, or g BST (back step) to move backward.
  • Go to a specific line: Press GTO . nnn where nnn is the line number.
  • Delete a line: Navigate to the line and press g ← (delete).
  • Insert: Simply navigate to where you want to insert and start typing. New instructions are inserted after the current line.

Running Programs

  1. Enter any required inputs onto the stack

    For example, if a program expects n in X and k in Y, type: n ENTER k

  2. Call the program by pressing GSB followed by the label

    For example: GSB A runs the program starting at LBL A

  3. For programs with multiple entry points, call the appropriate label:
    • GSB A might initialize
    • GSB 1 might iterate

Working with Registers

The HP-15C has storage registers R0 through R9, plus additional registers (R.0 through R.9) and index register I. Use them wisely:

  • STO n — Store X register into Rn
  • RCL n — Recall Rn into X register
  • STO+ n, STO- n, STO× n, STO÷ n — Arithmetic on registers
  • x<>y — Exchange X and Y registers
  • R↓ — Roll the stack down

Memory Management

The HP-15C shares memory between program storage and data registers. By default, you have:

  • Program memory: ~67 lines
  • Data registers: R0–R9 (expandable)

To allocate more registers (at the cost of program space):

  • f DIM (i) followed by the number of registers

To check memory status:

  • f MEM shows bytes remaining and register allocation

Saving Programs (Continuous Memory)

The HP-15C has continuous memory—programs and data survive when you turn the calculator off. As long as the batteries have power, your programs persist.

To protect against battery failure:

  1. Document your programs on paper. The original HP-15C came with program forms for this purpose.
  2. Use an emulator like Free42 (which emulates the HP-42S, a superset of the HP-15C) or dedicated HP-15C emulators. These can save and load program states.
  3. For the HP-15C CE (Collector's Edition) and newer reissues, some models support USB connectivity for backup.

Tips for Effective Programming

  1. Plan your register usage before coding. Write down which register holds what.
  2. Use the stack creatively. The 4-level stack (X, Y, Z, T) plus LastX register can often eliminate the need for storage registers.
  3. Test incrementally. Enter a few lines, exit program mode, test with sample inputs, then continue.
  4. Watch for label conflicts. Labels are global—if you load multiple programs that use LBL A, only the most recent one will be found.
  5. Use .0 through .9 labels for local subroutines to reduce conflicts with main program labels A-E.

The Joy of Constraints

There's a reason people still program these vintage calculators. It's not nostalgia (though that's part of it). It's not practicality (your phone is faster). It's something deeper.

When you program an HP-15C, you're forced to understand what you're doing. There's no library to import, no Stack Overflow answer to copy. You must comprehend the algorithm well enough to implement it in 20 lines or less. You feel every byte.

The programs in this collection aren't just mathematical curiosities. They're compressed wisdom—algorithms refined over centuries, now small enough to carry in your pocket. Euclid's GCD fits in 12 lines. Newton's method for square roots takes 15. The entire history of numerical mathematics, keystroke by keystroke.

The HP-15C is a reminder that computation is fundamentally about ideas, not clock cycles. The algorithms that matter—the ones that reveal something true about mathematics—don't require gigabytes of RAM. They require clarity.

And clarity, unlike memory, never becomes obsolete.

The complete program collection is available as an HTML reference document. Happy calculating.