Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Happy-numbers/PHP/happy-numbers.php
Normal file
24
Task/Happy-numbers/PHP/happy-numbers.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function isHappy($n) {
|
||||
while (1) {
|
||||
$total = 0;
|
||||
while ($n > 0) {
|
||||
$total += pow(($n % 10), 2);
|
||||
$n /= 10;
|
||||
}
|
||||
if ($total == 1)
|
||||
return true;
|
||||
if (array_key_exists($total, $past))
|
||||
return false;
|
||||
$n = $total;
|
||||
$past[$total] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$i = $cnt = 0;
|
||||
while ($cnt < 8) {
|
||||
if (isHappy($i)) {
|
||||
echo "$i ";
|
||||
$cnt++;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue