Skip to content

Instantly share code, notes, and snippets.

@sanikkenway
Created July 19, 2022 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanikkenway/e3c978977577c74d2cbd5ecb871e4972 to your computer and use it in GitHub Desktop.
Save sanikkenway/e3c978977577c74d2cbd5ecb871e4972 to your computer and use it in GitHub Desktop.
<?php
function kaprekar_constant($number)
{
$constant = 6174;
if (strlen((string)$number) == 3) {
$constant = 495;
}
do {
$arrays = array_map('intval', str_split($number));
rsort($arrays);
$reversed = implode('', $arrays);
sort($arrays);
$sorted = implode('', $arrays);
$number = $reversed - $sorted;
} while ($number != $constant);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment