# The difference 6174, a black hole

Mathematics is full of magic. Now stands the constant 6174 boasting about itself saying see the difference i can make.

1. Consider any natural number less than 10000 other than 1111*n where n = [1,9] i.e shouldn’t be an integral multiple of 1111.
2. Extract the largest four digit natural number that can be made by rearranging its digits with zeros included i.e 9 is considered as 0009
3. Similarly, find the smallest four digit natural number again by rearranging digits
4. Find the difference between largest and the smallest number , name it as result.
5. Consider result as the new number and repeat 2,3,4,5.

Woaaaaa and thats the amazing black hole here. You will reach the number 6174 in a maximum of seven steps and you cannot go any further from here.
Try it. I will do it for 9
Start from Number 9 = 0009
Iteration 1: 9000 – 0009 = 8991 = new number
Iteration 2: 9981 – 1899 = 8082 = new number
Iteration 3: 8820 – 0288 = 8532 = new number
Iteration 4: 8532 – 2358 = 6174 = new number
Iteration 5: 7641 – 1467 = 6174 = new number
Iteration 6: 7641 – 1467 = 6174 = new number
.
.

6174 reached at iteration 4.
Hey stop now, dont get in to the loop, you cannt go any further it ll give 6174 again
The number 6174 is known as Kaprekar’s Number

Run the following java code to ensure this: Click to Show/Hide

START

import javax.swing.JOptionPane;
public class Kaprekar {
public static void main(String[] args) {
int number, digit[] = { 0, 0, 0, 0 };
String msgString = “n”;
number = getInt4DigitInput(“Enter a natural number between 1 and 9998”
+ “n (excluding integral multiples of 1111)”);

if (number != 0) {
int[] sortedDigits;
int largest, smallest, count = 0;
do {
digit = number / 1000;
digit = number / 100 – digit * 10;
digit = number / 10 – digit * 10 – digit * 100;
digit = number – digit * 10 – digit * 100 – digit* 1000;
sortedDigits = sortInt(digit);
largest = generateLargest(sortedDigits);
smallest = generateSmallest(sortedDigits);
number = largest – smallest;
msgString += largest + ” – “ + smallest + ” = “ + number + “n”;
count++;
} while (number != 6174);
msgString += “Number 6174 reached in “ + count + ” iterations “
+ “nNumber reached = “ + number;
JOptionPane.showMessageDialog(null, msgString);
}
System.exit(0);
}

private static int generateSmallest(int[] sortedDigits) {
int smallest = 0;
for (int j = sortedDigits.length – 1; j >= 0; j–) {
smallest += sortedDigits[j] * Math.pow(10, j);
}
return smallest;
}

private static int generateLargest(int[] sortedDigits) {
int largest = 0;
for (int i = 0, j = sortedDigits.length – 1; i <>length; i++, j–) {
largest += sortedDigits[i] * Math.pow(10, j);
}
return largest;
}

private static int[] sortInt(int[] intArray) {
int temp;
for (int i = 0; i <>length; i++) {
for (int j = i + 1; j <>length; j++) {
if (intArray[i] <>
temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
}
}
}
return intArray;
}

public static int getInt4DigitInput(String inputMsg) {
int input = 0;
String returnValue = JOptionPane.showInputDialog(inputMsg);
if (null != returnValue) {
try {
input = Integer.parseInt(returnValue);
} catch (NumberFormatException nfe) {
input = getInt4DigitInput(“Please enter a valid four digit number,dont play around testing the code”);
}
if (input <> 9998) {
input = getInt4DigitInput(“Please enter a valid four digit number,didnt i tell you a number between 1-9998”);
} else if (input % 1111 == 0) {
input = getInt4DigitInput(“Please enter a valid four digit number,oops its a multiple of 1111, take care buddy”);
}
}
return input;
}
}

END

### 2 Responses

1. Sandeep says:

dude… why don’t you add some code to it, for anyone to play around. would be a nice addon to the post. 🙂

2. Sunil Kumar says:

done!