countthedivisors.php 424 B

123456789101112131415161718192021
  1. <?php
  2. function divisors($n) {
  3. // Shortcut evaluation for 1.
  4. if ($n === 1) {
  5. return 1;
  6. }
  7. $candidates = range(2, (int) ceil(sqrt($n)));
  8. $curr = $n;
  9. $divs = [1, $curr];
  10. while ($candidates) {
  11. $candidate = array_shift($candidates);
  12. if ($curr % $candidate == 0) {
  13. $divs[$candidate] = $candidate;
  14. $other = $curr / $candidate;
  15. $divs[$other] = $other;
  16. }
  17. }
  18. return count($divs);
  19. }