Count factors of given number n
ℹ️ © Codility, 2009-2018
Problem
A positive integer d is a “factor” of a positive integer n if there exists an integer m such that n = d · m.
For example, 6 is a factor of 24, because m = 4 satisfies the above condition (24 = 6 · 4).
Write a function that, given a positive integer n, returns the number of its factors.
For example, given n = 24, the function should return 8, because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24.
Assume that:
• n is an integer within the range [1 … 2,147,483,647].
Complexity:
• expected worst-case time complexity is O(sqrt(n));
• expected worst-case space complexity is O(1).
Solution
C#
using System; class Solution { public int solution(int n) { int r = 0; double t = (double)Math.Sqrt(n); int i = 1; while (i < t) { if (n % i == 0) { r += 2; } i++; } return r + (i == t ? 1 : 0); } }
Java
import java.lang.Math; class Solution { public int solution(int n) { int r = 0; double t = (double)Math.sqrt(n); int i = 1; while (i < t) { if (n % i == 0) { r += 2; } i++; } return r + (i == t ? 1 : 0); } }
JavaScript
function solution(n) { let r = 0; let t = Math.sqrt(n); let i = 1; while (i < t) { if (n % i == 0) { r += 2; } i++; } return r + (i == t ? 1 : 0); }
PHP
function solution($n) { $r = 0; $t = sqrt($n); $i = 1; while ($i < $t) { if ($n % $i == 0) { $r += 2; } $i++; } return $r + ($i == $t ? 1 : 0); }