PIM+PIMP
Display
(HP-41CX, Hewlett Packard 1983 and DM41X, SwissMicros 2020)
Overview
Two programs calculate π (pi) to N decimals with the formula of John Machin (1706): PIM and PIMP. PIM stores 5 decimals in every data register (base 100 000). PIMP is the packed version: it stores 10 decimals (two groups of 5) in every register, so about twice as many decimals fit in the same memory. Both programs set the SIZE themselves, show the maximum number of decimals when the memory is too small, measure the calculation time with the stopwatch and show π 10 decimals per line. The formulas used are:
π = 16·atan(1/5) − 4·atan(1/239)
atan(1/x) = 1/x − 1/3x3 + 1/5x5 − 1/7x7 + …
The series for 1/5 and 1/239 are added term by term until the term becomes zero. Both programs use the built-in X-Functions (PSIZE, SIZE?, ALENG) and Time functions (SETSW, RUNSW, STOPSW, RCLSW, ATIME24) of the HP-41CX and DM41X. More about the method: Machin-like formula (Wikipedia), Machin’s Formula (Wolfram MathWorld) and John Machin (Wikipedia).
Description
PIM keeps two multi-precision numbers in the data registers: T, the current term of the series, and S, the sum that becomes π. Each number takes n = INT(N/5)+3 registers of 5 decimals (SIZE = 2n+12); the first register holds the integer part and the last registers are guard digits, so N is rounded down to a multiple of 5. Subroutine LBL 01 adds ±T·atan(1/x) to S: T starts with the coefficient (16 or 4) and is divided by x (5 or 239), from then on by x2 for every term. Each term is also divided by 2k+1 and added to or subtracted from S, register by register, with the remainder carried to the next register. Registers of T that have become zero are skipped, so every term is faster than the previous one, and the series stops when T is zero. Finally the carries in S are normalised from right to left (MOD 100 000), the time is shown and π is displayed in groups of 5 with leading zeros.
PIMP works the same way, but packs two groups of 5 decimals into one register (R = hi·100 000 + lo). It needs n = INT((N+9)/10)+2 registers per number (SIZE = 2n+18), and N is rounded up to a multiple of 10. Every register is unpacked, divided group by group and packed again, so all intermediate values stay below 1010 and remain exact. S is kept in a “balanced” form (the high group may be negative) and a small carry goes to the register on the left; at the end one pass from right to left (MOD 1010) turns S into normal digits. The program is longer than PIM, but the number of decimals that fits in memory is almost doubled.
Metrics

All V41 times were measured with V41 at top speed and maximum turbo: PIM with 20 and 600 decimals and PIMP with 20 decimals. The other times (≈) are estimates: the run time grows with N2, and PIMP needs about 1.8 times as many program steps per decimal as PIM. The V41 at maximum turbo runs about 200 program steps per second and a real HP-41CX about 8, so the HP-41CX times are estimated as 25 times the V41 times. The DM41X has the same memory as the HP-41CX, so the maximum number of decimals is the same; it runs faster than the HP-41CX, but its times have not been measured yet.
Program Listing (PIM)
The listing of PIM is given below. The decimals are calculated first; the time is measured with the stopwatch. Only the global label PIM is used.

Program Listing (PIMP)
The listing of PIMP is given below. The decimals are calculated first; the time is measured with the stopwatch. Only the global label PIMP is used.

Registers, Labels and Flags (PIM)

Registers, Labels and Flags (PIMP)

Downloads
PDF format of programs PIM+PIMP.
RAW/TXT format of programs PIM+PIMP (in zip file).
© 2006-2026 by Auke Hoekstra



