Created
August 1, 2022 05:02
-
-
Save rzfarrell/bbc040b0fd044a3e285b2e344eb28908 to your computer and use it in GitHub Desktop.
Colorful Numbers Solution: https://algorithms.tutorialhorizon.com/colorful-numbers/
This file contains hidden or 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 isColorful($number) | |
{ | |
$number_as_string = strval($number); | |
$hash = array(); | |
$number_as_array = str_split($number); | |
foreach ($number_as_array as $int) | |
{ | |
if (isset($hash[strval($int)])) return false; | |
$hash[strval($int)] = 1; | |
} | |
$subsets = array(); | |
for ($i = 0; $i < count($number_as_array); $i++) | |
{ | |
for ($j = $i+1; $j < count($number_as_array); $j++) | |
{ | |
$subset = substr($number_as_string, $i, ($j-$i+1)); | |
if ($subset == $number_as_string) continue; | |
if (isset($subsets[$subset])) return 0; | |
$subsets[$subset] = 1; | |
$product = array_product(str_split($subset)); | |
if (isset($hash[strval($product)])) return 0; | |
$hash[strval($product)] = 1; | |
} | |
} | |
return 1; | |
} | |
echo isColorful(326); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment