Instantly share code, notes, and snippets.

Last active May 3, 2018 16:02
Show Gist options
• Save christophermanning/6468148 to your computer and use it in GitHub Desktop.
Kaprekar Routine

Created by Christopher Manning

## Summary

Kaprekar's constant is 6174. The Kaprekar Routine arranges four digits (zeros are appended to the number if it's less than 4 digits) in descending and ascending order, subtracts those two numbers, and repeats the process until the difference is 0 (degenrate case) or 6174 (Kaprekar's constant). The color is HSL with a scale of 0 to 300 in the domain of 1 to 7 (minimimum and maxium iterations to reach Kaprekar's constant) for hue, 1 for saturation, and .5 for lightness.

I created this because I wanted to visualize Kaprekar's constant in some way. This Wolfram MathWorld article on the Kaprekar Routine has a compelling image that is inspired by the cover of The Mathematics Teacher. From there, I decided to research some visualization techniques by recreating it with d3.js and adding some extra features (sorting, highlighting, and showing the routine).

I first heard of Kaprekar's constant after looking at the Google New York office. Apparently, there are about one-half of Kaprekar's constant Googlers there.

## References

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
 Kaprekar Routine
Sort by n or iterations