123456789101112131415161718192021 |
- <?php
- function divisors($n) {
- // Shortcut evaluation for 1.
- if ($n === 1) {
- return 1;
- }
- $candidates = range(2, (int) ceil(sqrt($n)));
- $curr = $n;
- $divs = [1, $curr];
- while ($candidates) {
- $candidate = array_shift($candidates);
- if ($curr % $candidate == 0) {
- $divs[$candidate] = $candidate;
- $other = $curr / $candidate;
- $divs[$other] = $other;
- }
- }
- return count($divs);
- }
|