Why does Kaprekar's constant work for different digit lengths?

  • Context: Undergrad 
  • Thread starter Thread starter Jonny_trigonometry
  • Start date Start date
  • Tags Tags
    Constant Mystery
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
Jonny_trigonometry
Messages
451
Reaction score
0
I just ran across this today:

http://www.trottermath.net/recurops/kaprekar.html

I find this pretty neat, so I just had to post it.

take any four digit number w/o repreated digits, and re-order from greatest to least, then subtract it's mirror image number from it and repeat, you will end up with 6174.

For three digit numbers, you will end up with 495.

for two digit numbers, you will end up in a period four cycle... I think.

I don't know how five digit numbers work. I just haven't really studied this idea for more than a half hour...

I've noticed that the three and four digit numbers add to 18, and since only 99 can add to 18 for two digit numbers, it must cycle through without landing on one specific number, since 99 doesn't have more than one arrangement, for that matter neither does 999 or 9999.

I wonder if the number 18 comes up due to the 10 base number system? maybe for any base, this thing will happen as long as the final number's digits all add to 2*(N-1) where N is the base of the number system, also provided it doesn't take repeated digits to add to that number?

I kinda want to make a program to map out all the four digit numbers, and find all the interations needed for each four digit number to reach 6174, negating the ones which won't work.
 
Mathematics news on Phys.org
It's 2:30 in the morning and I'm putting off chem homework that was due 2 weeks ago to do this... :rolleyes:
I have know idea why i just wrote this, but have fun with it. :wink:
why don't the newlines and tabs show up?
Code:
#include <stdio.h>

int Pow(int);

#define START 3600
#define END 3630
#define N 4
#define LIMIT 50

int main (void)
{
	int i, j, k, num, count, arr[N], temp, high, low;
	for (i = START; i <= END ; i++)
	{
		num = i;
		count = 0;
		while (num != 6174)
		{
			for (j = 0; j < N; j++)
			{
				arr[j] = num % 10;
				num = num / 10;
			}
			for (j = 0; j < (N - 1); j++)
			{
				for (k = 0; k < (N - 1 - j); k++)
				{
					if (arr[k] > arr[k+1])
					{
						temp = arr[k+1];
						arr[k+1] = arr[k];
						arr[k] = temp;
					}
				}
			}
			high = low = 0;
			for (j = 0; j < N; j++)
			{
				high += arr[j] * Pow(j);
				low += arr[j] * Pow(N - 1 - j);
			}
			num = high - low;
			//printf("%d, %d, %d\n", high, low, num);
			count++;
			if (count >= LIMIT)
			{
				printf("Exceeds limit...\n");
				count = -1;
				break;
			}
		}
		if (count >= 0) printf("%d\n", count);
	}
	return 0;
}

int Pow(int x)
{
	int val = 1, i = 0;
	while (i < x)
	{
		val = val * 10;
		i++;
	}
	return val;
}
 
Last edited:
durt said:
It's 2:30 in the morning and I'm putting off chem homework that was due 2 weeks ago to do this... :rolleyes:
I have know idea why i just wrote this, but have fun with it. :wink:
why don't the newlines and tabs show up?
Code:
#include <stdio.h>
int Pow(int);
#define START 3600
#define END 3630
#define N 4
#define LIMIT 50
int main (void)
{
int i, j, k, num, count, arr[N], temp, high, low;
for (i = START; i <= END ; i++)
{
num = i;
count = 0;
while (num != 6174)
{
for (j = 0; j < N; j++)
{
arr[j] = num % 10;
num = num / 10;
}
for (j = 0; j < (N - 1); j++)
{
for (k = 0; k < (N - 1 - j); k++)
{
if (arr[k] > arr[k+1])
{
temp = arr[k+1];
arr[k+1] = arr[k];
arr[k] = temp;
}
}
}
high = low = 0;
for (j = 0; j < N; j++)
{
high += arr[j] * Pow(j);
low += arr[j] * Pow(N - 1 - j);
}
num = high - low;
//printf("%d, %d, %d\n", high, low, num);
count++;
if (count >= LIMIT)
{
printf("Exceeds limit...\n");
count = -1;
break;
}
}
if (count >= 0) printf("%d\n", count);
}
return 0;
}
int Pow(int x)
{
int val = 1, i = 0;
while (i < x)
{
val = val * 10;
i++;
}
return val;
}


hmm... I'm not as savy in C as you are. It's kinda hard for me to follow, but I know that breaks can be avoided in almost all cases. Do you need the int Pow(int) to be global? by the way, what is Pow(int), a header file function in stdio.h? Do you need an & in front of printf values, or is that scanf? I like the program so far though. Good work!:smile:
 
Pow(int) returns 10^(parameter), and I defined it myself. There is a similar function, pow(), in math.h, but Pow() is so simple, I just tossed it in there (math.h is not needed now). Its scanf that you have to right to addresses. Anyway, you just define your start and end numbers, and the program will display the number of iterations needed to reach 6174. LIMIT is the maximum number of iterations the program will go through for a single number before giving up. This is a failsafe in case a number that never reaches 6174 occurs. All those for loops just assign the digits of num to an array, sorts the array (bubble sort), and converts the array back into the two numbers that have to be subtracted.

But perhaps all this has been in vain... http://kaprekar.sourceforge.net/proof/proof_r.php"
I don't get it, but then again I didn't read it...
 
Last edited by a moderator: