In C++, a K-number is an integer of from 1 to 4 digits in which at least one digit differs from the other digits. The Perfect K number is 6174. By following certain steps, one can transform any K-number to the Perfect K number. 0 is not a K-number. 1 is a K-number. 11 is a K-number. 222 is a K-number. 2222 is not a K-number. 12345 is not a K-number. A number greater than 9998 is not a K-number.
Write a program that generates random, non-negative integers between 0 and 9999 until a positive integer that is not a K-number appears. Transform each K-number into the Perfect K number by repeating these steps:
1. Make two copies of the K-number.
2. Arrange the first copy's digits in ascending order.
3. Arrange the second copy's digits in descending order.
4. Subtract the smaller number from the larger number.
For example, suppose the K-number is 1. From 1, we compute 1000 and 1. 1000 - 1 = 999. From 999, we compute 9990 and 999. 9990 - 999 = 8991. From 8991, we compute 9981 and 1899. 9981 - 1899 = 8082. From 8082, we compute 8820 and 288. 8820 - 288 = 8532. From 8532, we compute 8532 and 2358. 8532 - 2358 = 6174, the Perfect K number.