Last active
March 16, 2017 16:18
-
-
Save devNoiseConsulting/393d85f7ddd15c4b3209bf1ac0da6504 to your computer and use it in GitHub Desktop.
Kaprekar's Routine - PhillyDev Slack #daily_programmer - 20170308
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write a function that counts the number of iterations in Kaprekar's Routine, | |
which is as follows. | |
Given a 4-digit number that has at least two different digits, take that | |
number's descending digits, and subtract that number's ascending digits. For | |
example, given 6589, you should take 9865 - 5689, which is 4176. Repeat this | |
process with 4176 and you'll get 7641 - 1467, which is 6174. | |
Once you get to 6174 you'll stay there if you repeat the process. In this case | |
we applied the process 2 times before reaching 6174, so our output for 6589 is | |
2. | |
kaprekar(6589) -> 2 | |
kaprekar(5455) -> 5 | |
kaprekar(6174) -> 0 | |
Numbers like 3333 would immediately go to 0 under this routine, but since we | |
require at least two different digits in the input, all numbers will eventually | |
reach 6174, which is known as Kaprekar's Constant. | |
What is the largest number of iterations for Kaprekar's Routine to reach 6174? | |
That is, what's the largest possible output for your kaprekar function, given a | |
valid input? | |
*/ | |
function kaprekar(number) { | |
let total = number; | |
let count = 0; | |
if (uniqueDigitCount(total) > 1) { | |
while (total !== 6174) { | |
// Need to zero pad the total to avoid an infinite loop. | |
// ie 1000 => 1000 - 1 = 999 => 999 - 999 = 0 | |
let ascending = acendingNumber(("0000" + total).substr(-4, 4)); | |
let descending = decendingNumber(("0000" + total).substr(-4, 4)); | |
total = descending - ascending; | |
count++; | |
} | |
} | |
return count; | |
} | |
function uniqueDigitCount(number) { | |
// logic snagged from http://stackoverflow.com/questions/4833651/javascript-array-sort-and-unique/7076202#7076202 | |
let count = number.toString().split('').sort().filter(function(el, i, a) { | |
if (i == a.indexOf(el)) { | |
return 1; | |
} else { | |
return 0; | |
} | |
}); | |
return count.length; | |
} | |
function acendingNumber(number) { | |
return parseInt(number.toString().split('').sort(acendingSort).join('')); | |
} | |
function decendingNumber(number) { | |
return parseInt(number.toString().split('').sort(decendingSort).join('')); | |
} | |
function acendingSort(a, b) { | |
return a - b; | |
} | |
function decendingSort(a, b) { | |
return b - a; | |
} | |
let count = 0; | |
let number = (new Array(10)).fill([]); | |
for (let i = 1000; i < 10000; i++) { | |
let result = kaprekar(i); | |
if (result >= count) { | |
count = result; | |
number[count].push(i); | |
} | |
} | |
console.log(count, number[count].length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment