The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

Kaprekar numbers
Message #1 Posted by Thomas Klemm on 27 Sept 2010, 5:57 p.m.

There was that mini-challenge I started concerning Kaprekar's constant (6164) which directed me to something completely different: The Kaprekar Numbers.

Now there's this function Inv(a, b) which I faintly remembered is related to calculating gcd(a, b). It took me some time until I got all the pieces together again and realized that 3D vectors could be used to calculate it. So I dug out my HP-35s and entered the following program:

0001 -
0002 LASTx
0003 RTN

Press RTN to make sure you're at the beginning. Let's assume you want to calculate Inv(369, 271), so put the following two arrays on the stack:

         [369, 1, 0]
         [271, 0, 1]

Whenever the leftmost number in the top array is bigger than the associated number in the bottom array, press R/S. Otherwise swap x and y.

R/S
         [98, 1, -1]
         [271, 0, 1]         
x<>y
         [271, 0, 1]
         [98, 1, -1]
R/S
         [173, -1, 2]
         [98, 1, -1]
R/S
         [75, -2, 3]
         [98, 1, -1]
x<>y
         [98, 1, -1]
         [75, -2, 3]
R/S
         [23, 3, -4]
         [75, -2, 3]
x<>y
         [75, -2, 3]
         [23, 3, -4]
R/S
         [52, -5, 7]
         [23, 3, -4]
R/S
         [29, -8, 11]
         [23, 3, -4]
R/S
         [6, -11, 15]
         [23, 3, -4]
x<>y
         [23, 3, -4]
         [6, -11, 15]
R/S
         [17, 14, -19]
         [6, -11, 15]
R/S
         [11, 25, -34]
         [6, -11, 15]
R/S
         [5, 36, -49]
         [6, -11, 15]
x<>y
         [6, -11, 15]
         [5, 36, -49]
R/S
         [1, -47, 64]
         [5, 36, -49]

Finally we reached 1 = gcd(369, 271)!

Now you can represent 1 = -47 * 369 + 64 * 271

Thus Inv(271, 369) = 64 since 64 * 271 = 1 (mod 369) and Inv(369, 271) = -47 = 224 (mod 271) since -47 * 369 = 1 (mod 271).

As you can see the y and z component of the vectors keep track of the calculation.

However this algorithm gets more than tedious when the magnitude of both numbers differ as with 1010 and 7. What you can do is shift the smaller number to the left (i.e. multiplying with 10) until both magnitudes agree. And then shift it to the right again whenever it's too big to subtract.

Just wanted to note that the HP-35s has some useful features.

Cheers
Thomas

PS: Why would somebody want to calculate Inv(369, 271)?

Both 64 * 271 = 17344 and 224 * 369 = 82656 are Kaprekar numbers for n = 5 since:

173442 = 300814336; 3008 + 14336 = 17344

826562 = 6832014336; 68320 + 14336 = 82656


[ Return to Index | Top of Index ]

Go back to the main exhibit hall