Conditions | 4 |
Paths | 2 |
Total Lines | 23 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | public static function isHappy(int $n): bool |
||
10 | { |
||
11 | if ($n <= 0) { |
||
12 | return false; |
||
13 | } |
||
14 | $helper = static function (int $num) { |
||
15 | [$sum, $tmp] = [0, null]; |
||
16 | while ($num) { |
||
17 | $tmp = $num % 10; |
||
18 | $sum += $tmp * $tmp; |
||
19 | $num /= 10; |
||
20 | } |
||
21 | return $sum; |
||
22 | }; |
||
23 | |||
24 | $slow = $fast = $n; |
||
25 | do { |
||
26 | $slow = $helper($slow); |
||
27 | $fast = $helper($fast); |
||
28 | $fast = $helper($fast); |
||
29 | } while ($slow !== $fast); |
||
30 | |||
31 | return $slow === 1; |
||
32 | } |
||
48 |