Draft

Digital black hole

1 of 5trudbot

Description:

In number theory, Kaprekar's routine is an iterative algorithm that, with each iteration, takes a natural number in a given number base, creates two new numbers by sorting the digits of its number by descending and ascending order, and subtracts the second from the first to yield the natural number for the next iteration. It is named after its inventor, the Indian mathematician D. R. Kaprekar.

Kaprekar showed that in the case of four-digit numbers in base 10, if the initial number has at least two distinct digits, after seven iterations this process always yields the number 6174, which is now known as Kaprekar's constant.

such as 3124:

3124 -> 4321 - 1234 = 3087
3087 -> 8730 - 0378 = 8352
8352 -> 8532 - 2358 = 6174
6174 -> 7641 - 1467 = 6174
6174 -> 7641 - 1467 = 6174
...

This phenomenon is similar to a black hole (you can't get out after entering it), so 6174 is called a black hole number.

Task

n is a four digit number. Returns the path of the number n into the "black hole" 6174.

Example 1:

pathToBlackHole(3124) // should return {3124, 3087, 8352, 6174}
/*
3124 -> 4321 - 1234 = 3087
3087 -> 8730 - 0378 = 8352
8352 -> 8532 - 2358 = 6174
*/

Example 2:

pathToBlackHole(1000) // should return {1000, 999, 8991, 8082, 8532, 6174}
/*
1000 -> 1000 - 0001 = 0999
0999 -> 9990 - 0999 = 8991
8991 -> 9981 - 1899 = 8082
8082 -> 8820 - 0288 = 8532
8532 -> 8532 - 2358 = 6174
*/

Example 3:

pathToBlackHole(1) // should return {1, 999, 8991, 8082, 8532, 6174}
/*
0001 -> 1000 - 0001 = 0999
0999 -> 9990 - 0999 = 8991
8991 -> 9981 - 1899 = 8082
8082 -> 8820 - 0288 = 8532
8532 -> 8532 - 2358 = 6174
*/

Example 4:

pathToBlackHole(1111) // should return {}
// When n all digits are equal, it will not enter the black hole 6174. 
//In this case, please return an empty array
/*
1111 -> 1111 - 1111 = 0000
*/

More By Author:

Check out these other kata created by trudbot

Stats:

CreatedDec 5, 2022
Warriors Trained8
Total Skips0
Total Code Submissions12
Total Times Completed5
C++ Completions1
JavaScript Completions5
Total Stars0
% of votes with a positive feedback rating0% of 2
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes2
Total Rank Assessments3
Average Assessed Rank
8 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • trudbot Avatar
Ad