# Kaprekar's Constant 6174

 Posts 82
Casual Member
Ged
Casual Member
Joined: Aug 6 2005, 07:45 PM
I have just found this and I have tried it and it works! But is there a similar one for Dozenal? I tried four-digits and you get a round:-

Kaprekar's Constant 6174

1. Take any four-digit number except 1111, 2222, ..., 9999.

2. Arrange the digits in increasing and then decreasing order to get two four-digit numbers, add leading zeros if necessary.

3.Subtract the smaller number from the bigger number.

4. Repeat steps 2 and 3.

In at most 7 iterations, this process will ALWAYS produce 6174.
Dozenal numbers: or X for Ten, or E for Eleven said as Elv.

Member of :

DSGB http://www.dozenalsociety.org.uk/
DSA http://www.dozenal.org/

 Posts 179
Regular
jim
Regular
Joined: Apr 20 2012, 08:19 PM
I don't understand it but I see 9's.

7 cubed x 18 = 6174

Jim

wendy.krieger
wendy.krieger
In base 12, it seems to settle down to some cycles as shown. [CONFIRMED]

3EE8 - 8284 - 6376
7EE4 - 7375 - 4198 - 8374 - 5287 - 6196

Base 18 seems to do just two iterations of 3; [CONFIRMED]

10 1 15 8 - 14 1 15 4 - 14 9 7 4
6 1 15 12 - 14 5 11 4 - 10 5 11 8

In base 120, the cycles are shorter, giving eg (7260 cases to test).

0 0 0 0
72 23 95 48

40 23 95 80 - 72 39 79 48
88 7 111 32 - 104 55 63 16

88 23 95 32 - 72 55 63 48 - 24 7 111 96 - 104 71 47 16
88 39 79 32 - 56 39 79 64 - 40 7 111 80 - 104 39 79 16
88 55 63 32 - 56 7 111 64 - 104 7 111 16 - 104 87 31 16
88 71 47 32 - 56 23 95 64 - 72 7 111 48 - 104 23 95 16

This is interesting. If you add 1 1 0 to these numbers, they all correspond to fifteenths, but the first is just fifths, and the second is a composite of thirds and fifths. Each of the fifteenths ie 8n (n=1 to 14) occur exactly 6 times.

Here is the rexx script if you want to play with it.

Even more interesting, is that 6174 + 110 = 6284, and this is 3/5 1/5 4/5 2/5. That's exactly the same as twelfty's 72 23 95 48, and indeed, the same formation works in all bases which are multiples of 5, eg

20 gives 12 3 15 8. 15 gives 9 2 11 6 etc.

Code: Select all

``````/* rexx */
parse arg base tail; sz=0;
do forever; Â sz=sz+1
Â parse var tail s.sz tail; s.sz = s.sz+0
Â if tail = '' then leave; end;
do ll = 1 to 22
call sortme
call kaprekar
ax = format&#40;ll, 4&#41;'&#58;'
do a=1 to sz; ax = ax s.a; end
say ax
end

exit

sortme&#58;
do b=1 to sz; t.b = -1 Â
do a=1 to sz; t.b = max&#40;t.b, s.a&#41;; end
do a=1 to sz; if s.a < t.b then iterate; s.a = -1; leave; end
end&#59;
return
kaprekar&#58;
do a=1 to sz; aa = sz+1-a; s.a = t.a - t.aa; end
do a=1 to sz; ab = sz-a; aa = ab+1;
Â  if s.aa < 0 then do; s.aa = s.aa + base; s.ab = s.ab -1; end
Â  end;
return
``````
KAPREKAR.REX base d0 d1 ....
eg k 10 7 1 6 5

This sorts the digits into ascending and descending order, eg 7 6 5 1 and 1 5 6 7, and subtracts them (6, 1, -1, -6). The negative digits are eliminated as in hand arithetic, by adding base, and subtract 1 from the next column.

This is iterated and printed LL times. 22 seems a suited value on a 25-line screen.

Dozens Demigod
dgoodmaniii
Dozens Demigod
Joined: May 21 2009, 01:45 PM
Ged @ Feb 19 2015, 01:08 PM wrote: I have just found this and I have tried it and it works! But is there a similar one for Dozenal?
I'm writing a program to test this. I'll report the results tonight; I'm halfway through and need to do something else. But this looks like fun.
All numbers in my posts are dozenal unless stated otherwise.
For ten, I use or X; for elv, I use or E. For the digital/fractional/radix point, I use the Humphrey point, ";".
TGM for the win!

wendy.krieger
wendy.krieger
Base 40 settles down to a single value, while base 55 has a period of 20 items.

Base 120 can be used to derive 15. Add 1.1.0 to each element of the cycle, divide the digits by 8, and then subtract 1.1.0, eg

88.71.47.32 gives 88,72,48,32 gives 11,9,6,4 gives 11,8,5,4

Dozens Demigod
dgoodmaniii
Dozens Demigod
Joined: May 21 2009, 01:45 PM
Programs used to produce the following are at the bottom of this post.

First off, I could find no pattern for four-digit numbers in dozenal. I ran 100; different numbers for 10; iterations each and produced no patterns; indeed, the number of duplicates was lower than I would expect even from chance. (Not actually, of course, but it surprised me.) Of course, I may have made some horrible coding mistake to make it not work, but it did work for the decimal version pretty smoothly, so I don't think I have.

For five digit numbers, however, there was a definite pattern, and the number is 83E74. I haven't jiggered the program to record the number of iterations necessary to produce this, but only five of the 100; different trial numbers (randomly selected) failed to produce this number. I have not yet investigated if these are simply five identical digits, which the rules originally suggested specifically exclude, or what. I hope to learn more about this later tonight when I get some more leisure time.

So, in conclusion: 5-digit numbers, 83E74.

The below program is "check.sh"; it takes the number offered as its first argument, does no error-checking whatsoever, calls a function to split the given number into itself arranged ascending and descending, then does the necessary subtraction, prints the result, and loops again. Now that I have an idea what the right number is, I'll probably tell it to stop when it reaches said number and report the number of iterations necessary to reach it; until then, here it is.

Code: Select all

``````#/bin/bash
# +AMDG

num=&#036;1;
function rearrange
{
digits=`echo &#036;num &#124; grep -o .`;
digits=`echo "&#036;digits" &#124; sed -e 's/\&#40;&#91;0-9&#93;\&#41;/ 0\1 /g'`
digits=`echo "&#036;digits" &#124; sed -e 's/X/10/g'`
digits=`echo "&#036;digits" &#124; sed -e 's/E/11/g'`
ascdigs=`echo &#036;digits &#124; awk 'BEGIN{RS=" ";} {print &#036;1}' &#124; sort`
descdigs=`echo &#036;digits &#124; awk 'BEGIN{RS=" ";} {print &#036;1}' &#124; sort -r`
ascdigs=`echo &#036;ascdigs &#124; sed -e 's/10/X/g'`
ascdigs=`echo &#036;ascdigs &#124; sed -e 's/11/E/g'`
descdigs=`echo &#036;descdigs &#124; sed -e 's/10/X/g'`
descdigs=`echo &#036;descdigs &#124; sed -e 's/11/E/g'`
ascdigs=`echo &#036;ascdigs &#124; sed -e 's/0\&#40;&#91;0-9&#93;\&#41;/\1/g'`
descdigs=`echo &#036;descdigs &#124; sed -e 's/0\&#40;&#91;0-9&#93;\&#41;/\1/g'`
ascdigs=`echo &#036;ascdigs &#124; sed -e 's/ //g'`
descdigs=`echo &#036;descdigs &#124; sed -e 's/ //g'`
}
for i in `seq 1 12`; do
rearrange
decasc=`dec &#036;ascdigs &#124; cut -d. -f1`;
decdesc=`dec &#036;descdigs &#124; cut -d. -f1`;
if &#91;&#91; &#036;decasc -gt &#036;decdesc &#93;&#93;; then
&nbsp;num=`dozdc " &#036;ascdigs &#036;descdigs - ="`;
else
&nbsp;num=`dozdc " &#036;descdigs &#036;ascdigs - ="`;
fi
echo "&#036;num"
done
exit
``````
The below program is "rollcheck.sh," which is bash rather than simply POSIX shell so I can use the convenient random-number function. (Yes, I could get them from /dev/random; but whaa.) It simply produces a random number; makes sure that it's really only five digits (by piping it through "cut"), and feeds it to check.sh, writing the results therefore into a file called "results." At the end, it extracts all the duplicate entries, prints them to the screen, then writes them to a file called "commons."

Code: Select all

``````#!/bin/bash
# +AMDG

rm results;
for i in `seq 1 144`; do
trialnum=&#036;&#40;&#40;RANDOM%248831+20735&#41;&#41;
number=`doz &#036;trialnum`
number=`echo &#036;number &#124; cut -c1-5`
./check.sh &#036;number >> results;
done;
uniq -cd results;
uniq -cd results >> commons;
``````
(I promise that on my computer this code is indented sensibly; the code environment on the board doesn't retain that.)
All numbers in my posts are dozenal unless stated otherwise.
For ten, I use or X; for elv, I use or E. For the digital/fractional/radix point, I use the Humphrey point, ";".
TGM for the win!

wendy.krieger
wendy.krieger
Don's program was looking for a single value, rather than a loop. You could use an associative array to detect loops.

My program prints out 20 consecutive iterations of the process, and you can see in that the full iterations of either of the two loops. In fact, all of the data was cut from a command prompt, and edited to make it line.

But in any case, there really isn't a lot of numbers to check. The base forms are a, b, -b, -a, where a \ge b \ge 0. You then apply the carry ripple through it.

Dozens Demigod
dgoodmaniii
Dozens Demigod
Joined: May 21 2009, 01:45 PM
I'm sorry, I don't understand what you're saying here.

Looking for a loop? It uses a loop to seek a single value; namely, the common result of this series of arithmetic operations. I'm not sure why I'd be looking for a loop, nor even what precisely that would mean in this context.
But in any case, there really isn't a lot of numbers to check.&nbsp; The base forms are a, b, -b, -a,&nbsp; where a \ge b \ge 0.&nbsp; You then apply the carry ripple through it.
Aren't there, in fact, a lot of numbers to check? Namely, 10000--EEEEE? I'm not sure what you mean by "carry ripple", either.
All numbers in my posts are dozenal unless stated otherwise.
For ten, I use or X; for elv, I use or E. For the digital/fractional/radix point, I use the Humphrey point, ";".
TGM for the win!

 Posts 179
Regular
jim
Regular
Joined: Apr 20 2012, 08:19 PM
You guys are not looking at this as an ancient problem and it is a pyramid problem and it is about cubes.

In the ancient world to work out the side of a cube double that of 7 was not to use 1.25992 as it took too much calculation. 1.26 was used.

A cube twice the volume of 7 343 is 686 and the cube root is 8.81944 The ancients used 1.26 so the cube root = 8.82.

8.82 x 7 = 61.74

Then interestingly 7 squared 49 x 1.26 the approximate for the cube root of 2 = 61.74.

Jim

wendy.krieger
wendy.krieger
There are not many numbers to check at all. For example, for four and five place dozenal numbers, there are just 78, and all fall under 100;.

Consider what happens when you try 7165. The process is to sort the digits and subtract the ascending from the descending, eg 7651 - 1567. But we can subtract 1551 from these to get 6100 - 0016. So the outcome of 7165 is the same as 0016, or 0061 etc.

So, the loops are then

Code: Select all

``````for a = 0 to b-1 do
for b = 0 to a Â do
Â  call try_this_number
end
end
``````
suffices for all four- and five- place numbers, since the middle number disappears.

The ripple carry is in my code, which first finds 7 6 5 1 - 1 5 6 7 = 6 1 -1 -6. The next step is to do the carry: ie -6 = -1 6, carry the one to -2 1 -2 = 0 10 So this is 6 0 10 6, dozenal, or 6 0 8 4 decimal, etc.

I ran through the base 12 five place numbers, and 74 of the 78 end up at 83E74 (which corresponds to the next iteration as 0 0 0 1 1), but there are three irregulars, 00006, 00007 and 00056, which ends at 6EEE5 - 64E66, and the irregular 00000 which gives itself. The four-place numbers all end up in the loop aboves.

40 gives 24.07.31.16 after no more than 21 iterations, except for x.x.x.x which leads to 0.0.0.0. 8.24 is the smallest number to give this straight up.

10 gives 6174 after no more than 7 iterations, except for NNNN, 26 is the smallest number to give this straight up.

Dozens Disciple
Dan
Dozens Disciple
Joined: Aug 8 2005, 02:45 PM
dgoodmaniii @ Feb 21 2015, 10:09 PM wrote:Looking for a loop?&nbsp; It uses a loop to seek a single value; namely, the common result of this series of arithmetic operations.&nbsp; I'm not sure why I'd be looking for a loop, nor even what precisely that would mean in this context.
I think she means a repeating cycle of numbers. For example, in the middle-square PRNG method with 4 decimal digits, the sequence 0540 â†’ 2916 â†’ 5030 â†’ 3009 â†’ 0540 â†’ 2916 â†’ 5030 â†’ 3009...

wendy.krieger
wendy.krieger
No, Jim, we're not looking at any particular property of 6174, except that it is a set of digits with interesting properties. That is, we're not looking at the number 1.1.1.0 base 18 (ie 7*7*7*18), or any of the facinating relations between 63 and 50 and their cubes.

What we're looking at is the sort of thing that people who don't use other bases and have too much free time on their hand do. They somehow think that there is some sort of magic in the decimal expansion of pi, or tricks with digits.

Here, the number in question is derived from eg 6200 - 0026, or other numbers by adding symmetric numbers, like 1221 to both sides, eg 7421 - 1247.

Of course, people notice these tricks, and say, Oi! what about dozenal? What about a hundred. Doesn't Kaprekar's process give a single value anywhere?

So we do a bit of pan-base hacking. Don's code seems to be largely base 12, where as mine is set for any base, such as base 120. In fact, you set the base in the first param.

Dozens Disciple
Dan
Dozens Disciple
Joined: Aug 8 2005, 02:45 PM
What we're looking for is a fixed point of the function that maps one number in the Kaprekar sequence to the next.

For example, in decimal, 6174 maps to 7641 - 1467 = 6174 again.

Through a brute-force search, I've found that the only such fixed point with 4 dozenal digits is 0000.

But it does seem that many initial "seed" values lead into the loop 8374 â†’ 5287 â†’ 6196 â†’ 7BB4 â†’ 7375 â†’ 4198 â†’ 8374 â†’ ...

wendy.krieger
wendy.krieger
This list shows the kaprekar iterations of 0 0 1 2 and 0 0 0 3 in base 12. These settle down to loops, such as from 3: to 9: in the first example, and 4: to 7: in the second example. All 78 root values have been checked, and apart from 0000, they all end in one of these two loops.

Code: Select all

``````&#91;H&#58;\USERS\Wendy&#93;kaprekar 12 0 0 1 2
Â  1&#58; 2 0 10 10
Â  2&#58; 10 7 3 2
Â  3&#58; 8 3 7 4
Â  4&#58; 5 2 8 7
Â  5&#58; 6 1 9 6
Â  6&#58; 7 11 11 4
Â  7&#58; 7 3 7 5
Â  8&#58; 4 1 9 8
Â  9&#58; 8 3 7 4
Â 10&#58; 5 2 8 7
Â 11&#58; 6 1 9 6
Â 12&#58; 7 11 11 4

&#91;H&#58;\USERS\Wendy&#93;kaprekar 12 0 0 0 3
Â  1&#58; 2 11 11 9
Â  2&#58; 9 1 9 3
Â  3&#58; 8 5 5 4
Â  4&#58; 3 11 11 8
Â  5&#58; 8 2 8 4
Â  6&#58; 6 3 7 6
Â  7&#58; 3 11 11 8
Â  8&#58; 8 2 8 4
Â  9&#58; 6 3 7 6
``````
There are 78 root values for the five-digit case, of which 74 settle down to a single value 83E74. One settles down to 00000, and three settle down to the second loop. These are 00006, 00007, and 00056.

All 78 values have been followed through.

Code: Select all

``````&#91;H&#58;\USERS\Wendy&#93;kaprekar 12 0 0 0 1 2
Â  1&#58; 2 0 11 10 10
Â  2&#58; 11 7 11 3 1
Â  3&#58; 10 7 11 3 2
Â  4&#58; 9 6 11 4 3
Â  5&#58; 8 4 11 6 4
Â  6&#58; 7 3 11 7 5
Â  7&#58; 8 1 11 9 4
Â  8&#58; 10 4 11 6 2
Â  9&#58; 9 5 11 5 3
Â 10&#58; 8 3 11 7 4
Â 11&#58; 8 3 11 7 4
Â 12&#58; 8 3 11 7 4
Â 13&#58; 8 3 11 7 4
Â 14&#58; 8 3 11 7 4

&#91;H&#58;\USERS\Wendy&#93;kaprekar 12 0 0 0 0 6
Â  1&#58; 5 11 11 11 6
Â  2&#58; 6 4 11 6 6
Â  3&#58; 6 11 11 11 5
Â  4&#58; 6 4 11 6 6
Â  5&#58; 6 11 11 11 5
Â  6&#58; 6 4 11 6 6
``````
Early tests on the six-place dozenal suggests one or more loops of ten (ie 2*5). Some have a leadin of 20 places, which means i will need to think differently here.

The 2-place version has a period of 6 (the six odd multiples of E), while the three-digit version ends at 5E6. The seven-digit version has a period of 9, or some multiple of it.

Likewise the 820 root values for base 40 have all been done, all give 24 7 31 16, with the exception of when all four digits are the same, these give 0 0 0 0. The longest lead is 20, meaning that the number appears only at the 21 iteration.

The reason why i was suggesting an associative array, is because the return to the start of the loop is immediately detected when one stores a value using the number.

For example, in the first four-digit 12 example above, the script would store the value (before the colon) in the label (after the colon), eg a."2 0 10 10" = 1, etc. But when it gets to line 9, it looks for a."8 3 7 4" but this already has 3 in it, so there are 2 leaders (ie 3-1), and a loop of 6 (ie 9-3).

wendy.krieger
wendy.krieger
For 120, the following

2-digit: There is a period of 55, except for multiples of 11, which give 5.

3-digit: All settle down to 59.E9.60, but the leadins can be quite long.

4-digit: Tested loops settle down to one of the four-period places.

5-digit: These seem to head to 80.39.E9.79.40, but the experience with 12 tells us not to suppose all do this.

 Posts 179
Regular
jim
Regular
Joined: Apr 20 2012, 08:19 PM
Hi Wendy

What if we tried 2592? I think we would do it in 7?

Jim

Dozens Demigod
dgoodmaniii
Dozens Demigod
Joined: May 21 2009, 01:45 PM
Yes, my scripts were entirely dozenal, by design. And yes, I wasn't really interested in finding cycles, but in locating a recurring value.

I still don't really understand what Wendy means by a "carry ripple," but yes, I found through brute force attack that the five-digit pattern isn't as clean as it had originally looked. It's really two patterns, one that ends at 83E74, and the other that loops at 6EEE5 - 64E66.

On the other hand, with three digits, I found that nearly all ended in 5E6 (this is a small enough number that I could run every one without growing old waiting for it to finish). Out of XEE possibilities, X5X ended in 5E6.

There doesn't seem to be any number of dozenal digits that works as cleanly as 6174 in decimal, though.
All numbers in my posts are dozenal unless stated otherwise.
For ten, I use or X; for elv, I use or E. For the digital/fractional/radix point, I use the Humphrey point, ";".
TGM for the win!

wendy.krieger
wendy.krieger
Carry ripple, is simply that when you subtract, eg 6200 from 0026, you have to do several carries. The term 'ripple' is a hardware term, that if you have several addition gates, the final result isn't stable until the carry has 'rippled' through the units, tens, hundreds gates.

In the example above, subtracting 26 from 6200 causes a carry in the units, tens, and hundeds column, so the thing isn't final until that is done. After the uncarried subtraction is done of pairs of numbers, one is left with eg a, b, -b, -a, where b <= a. This is eaily achieved from a starting from eg "0 0 b a" directly. This is why i only looked at these numbers.

My rexx script (i upated the image in my first post), actually handles all bases, and any number of digits. The sort method is primitive, (it's a kind of insertion sort), but effective for very small numbers. On the other hand, the killer for larger values is that the leadin to the period can be quite long. For example, for three places of 120, it does settle down to 59.119.60, but this can take sixty iterations to get there.

From running the script on four-place dozenal (kaprekar 12 0 0 0 1) it came apparent that it settles down to a cycle of three or six places. When Don said that some of the five-place dozenal gave no result, i could from my runthroughs that these typically referred to the cycle-two cases, 00005, 00006 and 00056.

 Posts 82
Casual Member
Ged
Casual Member
Joined: Aug 6 2005, 07:45 PM
Dgoodmaniii I thought you had got it with the 5 digit 83E74. Then I saw that you found 6EEE5 - 64E66.
So it looks like this phenomena only works for decimal. How nice it would have been if it worked for other bases with different number of digits e.g. half the base minus one.
That would have been brilliant. Thanks for trying.
Dozenal numbers: or X for Ten, or E for Eleven said as Elv.

Member of :

DSGB http://www.dozenalsociety.org.uk/
DSA http://www.dozenal.org/