The Museum of HP Calculators


Kaprekar Series for the HP-41

This program is Copyright © 2007 by Jean-Marc Baillard and is used here by permission.

This program is supplied without representation or warranty of any kind. Jean-Marc Baillard and The Museum of HP Calculators therefore assume 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.

Overview

-Take for example a 3-digit number, say  N0 = 286
-Sort the digits in increasing order:  268  and in decreasing order:  862
-Calculate the difference  862 - 268 = 594 = N1

-Repeat this operation:  954 - 459 = 495 = N2
-Again:  954 - 459 = 495 = N3

-The Kaprekar series in this example is  286 , 594 , 495 , 495 , .....
-So we end up in a 1-number cycle:  { 495 }

-The following programs allows to find such cycles.
 

Program Listing
 

Data Registers:           •  R00 = n = number of digits  ( 1 <= n <= 10 )                        ( Register R00 is to be initialized before executing "KAPR"  )
                                         R01 thru R10 = digits of the numbers    R11 & R12: temp

                                     -  R13  R14  R15  .....................  =  N0  N1  N2  .............................
Flags: /
Subroutine:   "SORT"  ( cf "Sorting Numbers for the HP-41" )
 

01  LBL "KAPR"
02  VIEW X
03  12.012
04  STO 12
05  X<>Y
06  LBL 01
07  RCL 00
08  X<>Y
09  ISG 12
10  CLX
11  STO IND 12
12  LBL 02
13  RCL X
14  10
15  MOD
16  STO IND Z
17  -
18  10
19  /
20  DSE Y
21  GTO 02
22  RCL 00
23  STO 11
24   E3
25  /
26  ISG X
27  XEQ "SORT"               or a M-code routine SORT ( for instance the one listed in "Sorting Numbers for the HP-41" )
28  SIGN
29  0
30  ENTER^
31  LBL 03
32  10
33  ST* Z
34  *
35  RCL IND Z
36  +
37  X<>Y
38  RCL IND 11
39  +
40  X<>Y
41  ISG Z
42  CLX
43  DSE 11
44  GTO 03
45  -
46  VIEW X
47  RCL 12
48  X<>Y
49  LBL 04
50  X=NN?                             If you don't have an HP-41CX,    replace line 56 by  RCL 11
51  GTO 05                                                                                -----------  52 by  DSE 11
52  DSE Y                                                                                  -----------  50 by  CLX   RCL IND 11   X=Y?
53  GTO 04                                                                                -----------  48 by  STO 11
54  GTO 01                                                                                -----------  08 by  RCL Z
55  LBL 05                                                                             and delete line 05
56  X<>Y
57  INT
58  RCL 12
59  INT
60   E3
61  /
62  +
63  CLD
64  END

( 108 bytes / SIZE ? )
 
 
      STACK         INPUT       OUTPUT
           X            N0        bbb.eee

   where  bbb.eee  is the control number of the cycle

Example1:     n = 6  &   N0 = 918682

     6       STO 00
918682  XEQ "KAPR"  >>>>  the HP-41 displays  N0 ,  N1 ,  N2 , ...... and finally gives  24.030   ( in 4mn02s )

-So we eventually end up in a 7-number cycle in registers  R24 , ..... , R30  namely:

       { 851742 , 750843 , 840852 , 860832 , 862632 , 642654 , 420876 }
 

Example2:     n = 5  &   N0 = 4    which will be read  "00004"  since  n = 5

    5   STO 00
    4     R/S        >>>>   15.018   ( in 61 seconds )

-The 4-number cycle is in registers  R15 thru R18:

       { 62964 , 71973 , 83952 , 74943 }
 
 
 

Go back to the HP-41 software library
Go back to the general software library
Go back to the main exhibit hall