Created
July 19, 2022 02:04
-
-
Save sanikkenway/e3c978977577c74d2cbd5ecb871e4972 to your computer and use it in GitHub Desktop.
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
<?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