This variation allow infinite numbers to be tested.
function dividedByThree(int $number): bool { while(strlen($number) > 1) { $number = abs(array_sum(str_split($number))); } switch ($number) { case 3: case 6: case 9: return true; break; default: return false; } }
- function dividedByThree(int $number): bool
- {
$abs = abs($number);if($abs < 10){return $abs == 3 || $abs == 6 || $abs == 9;- while(strlen($number) > 1) {
- $number = abs(array_sum(str_split($number)));
- }
- switch ($number) {
- case 3:
- case 6:
- case 9:
- return true;
- break;
- default:
- return false;
- }
$sum = array_sum(str_split($abs,1));return dividedByThree($sum);- }