Generates a plot of total cycle depth for the Kaprekar Routine for the range [0, 10k). Also see the heavyweight 1M version here.
A Pen by Riley Shaw on CodePen.
canvas#canvas width=400 height=400 |
Generates a plot of total cycle depth for the Kaprekar Routine for the range [0, 10k). Also see the heavyweight 1M version here.
A Pen by Riley Shaw on CodePen.
;(function () { | |
function step (cur, prev, i) { | |
if (prev[cur]) return i - 1; | |
else prev[cur] = true; | |
var lo = String(cur).split('').sort(); | |
var hi = lo.slice().reverse(); | |
lo = +(lo.join('')); hi = +(hi.join('')); | |
return step(hi - lo, prev, i + 1); | |
} | |
function kaprekar (num) { | |
return step(num, {}, 0); | |
} | |
var ctx = document.getElementById('canvas').getContext('2d'); | |
var colors = ['rgb(236, 240, 241)', 'rgb(209, 215, 218)', 'rgb(181, 189, 195)', 'rgb(154, 164, 172)', 'rgb(126, 138, 149)', 'rgb(99, 113, 126)', 'rgb(71, 87, 103)', 'rgb(44, 62, 80)']; | |
for (var i = 0; i < 10000; i++) { | |
ctx.fillStyle = colors[kaprekar(i)]; | |
ctx.fillRect((i % 100) * 4, Math.floor(i / 100) * 4, 4, 4); | |
} | |
})(); |
body | |
background: rgb(44,62,80) | |
canvas | |
position: absolute | |
top: 50% | |
left: 50% | |
margin: -200px |