Skip to content

Instantly share code, notes, and snippets.

@rzfarrell
Created August 1, 2022 05:02
Show Gist options
  • Save rzfarrell/bbc040b0fd044a3e285b2e344eb28908 to your computer and use it in GitHub Desktop.
Save rzfarrell/bbc040b0fd044a3e285b2e344eb28908 to your computer and use it in GitHub Desktop.
<?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