PIEP+PIEX

Display

(HP-41CX, Hewlett Packard 1983 and DM41X, SwissMicros 2020)
 

Overview
Two programs calculate π (pi) to N decimals with the series of Leonhard Euler (1755): PIEP and PIEX. Both store 10 decimals (two groups of 5) in every data register, like PIMP, but they need only one multi-precision number instead of two, so about twice as many decimals fit in the same memory. PIEP uses main memory only; PIEX also puts the part of the number that does not fit in main memory in an X-memory data file, which allows thousands of decimals. 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:
    π = 2 + 1/3·(2 + 2/5·(2 + 3/7·(2 + 4/9·(2 + …))))
    π/2 = 1 + 1/3 + 1·2/(3·5) + 1·2·3/(3·5·7) + …

The series is evaluated from the inside out (Horner form): starting with the deepest term, the number is multiplied by k/(2k+1) and 2 is added, for k = K down to 1. Every term adds about one bit, so K ≈ 3.32·N terms are needed. Both programs use the built-in X-Functions (PSIZE, SIZE?) and Time functions (SETSW, RUNSW, STOPSW, RCLSW, ATIME24) of the HP-41CX and DM41X; PIEX also uses the X-memory functions (EMROOM, PURFL, CRFLD, SEEKPT, GETRX, SAVERX, GETX). More about the method: Spigot algorithm (Wikipedia), Pi Formulas (Wolfram MathWorld) and Leonhard Euler (Wikipedia).
 

Description
PIEP keeps one multi-precision number Y in the data registers, packed with two groups of 5 decimals per register (R = hi·100 000 + lo) as in PIMP. It needs n = INT((N+9)/10)+2 registers (SIZE = n+12); the first register holds the integer part and the last one is a guard register, and N is rounded up to a multiple of 10. Y starts at 2; then for k = K down to 1 the program computes Y = 2 + k·Y/(2k+1) in one pass from left to right (LBL 03): every register is unpacked, multiplied by k and divided by 2k+1 group by group with the remainder carried to the next group, so all intermediate values stay below 1010 and remain exact. The groups are kept in a “balanced” form and a small carry goes to the register on the left. A later term only influences about N − 0.301·k decimals, so the passes for large k stop early; this halves the run time. Finally one pass from right to left (MOD 1010) turns Y into normal digits, the time is shown and π is displayed 10 decimals per line.
 
PIEX does the same calculation, but first takes the largest SIZE that fits. The first M registers of Y stay in main memory; the rest is kept in the X-memory data file “PIX” (an old file is purged first and the new file stays until the next run). Each pass works through the file in blocks of 20 records: SEEKPT and GETRX read a block into a buffer, the block is calculated and SAVERX writes it back. The buffer keeps the last register of the previous block, so the carry to the left also works across block boundaries. PIEX needs at least SIZE 44 (R00–R21, the buffer and two registers of Y); with less main memory it shows MAX=0. The maximum number of decimals depends on the free X-memory (EMROOM); the method itself is exact up to about 9 000 decimals, because (2k+1)·100 000 must stay below 1010.
 

Metrics

All times are estimates (≈) from the number of program steps, which grows with N2. The V41 at top speed and maximum turbo runs about 200 program steps per second (measured with PIM and PIMP) and a real HP-41CX about 8, so the HP-41CX times are estimated as 25 times the V41 times. The Euler series needs about 3.3 terms per decimal, so PIEP and PIEX are slower than PIMP for the same N, but much more decimals fit in memory. The PIEX maximum on the V41 and DM41X assumes 600 registers of X-memory (as an HP-41CX with two Extended Memory modules); an HP-41CX without modules has 124 registers of X-memory, which gives 2900 decimals. The DM41X runs faster than the HP-41CX, but its times have not been measured yet.
 

Example (1)

 

Example (2)

 

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

 

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

 

Registers, Labels and Flags (PIEP)

 

Registers, Labels and Flags (PIEX)

 

Downloads
PDF format of programs PIEP+PIEX.
RAW/TXT format of programs PIEP+PIEX (in zip file).
 

This program is copyright and is supplied without representation or warranty of any kind. The author assumes no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof

© 2006-2026 by Auke Hoekstra