Another quick PHP one. Betting there is a more performant method but will leave that to someone else. :)
<?php function isHappyNumber ($oldNumber) { $numArray = str_split($oldNumber); $newNumber = 0; foreach ($numArray as $val) { $newNumber += $val*$val; } if ($newNumber === 1) { return true; } else if ($newNumber === 4) { return false; } else { return isHappyNumber($newNumber); } } $happyNumbers = array(); for ($i = 1; $i < 100; $i++) { if (isHappyNumber($i)) { $happyNumbers[] = $i; } } print_r($happyNumbers); ?>
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another quick PHP one. Betting there is a more performant method but will leave that to someone else. :)