C++ Count Factors
long long countFactors(long long n)
{
long long count = 0;
long long i = 1;
while (i * i < n) {
if (n % i == 0) {
count += 2;
}
++i;
}
if (i * i == n) {
++count;
}
return count;
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
C# Count Factors
static int CountFactors(long n)
{
var count = 0;
long i = 1;
while (i * i < n)
{
if (n % i == 0)
{
count += 2;
}
i++;
}
if (i * i == n)
{
count++;
}
return count;
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Elixir Count Factors
defmodule CountFactors do
def count_factors(n), do: loop(1, 0, n)
defp loop(i, count, n) when i * i < n do
count = if rem(n, i) == 0, do: count + 2, else: count
loop(i + 1, count, n)
end
defp loop(i, count, n) do
if i * i == n, do: count + 1, else: count
end
end
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Erlang Count Factors
-module(count_factors).
-export([count_factors/1]).
count_factors(N) ->
count_factors(N, 1, 0).
count_factors(N, I, Count) when I * I < N ->
NewCount = case N rem I =:= 0 of
true -> Count + 2;
false -> Count
end,
count_factors(N, I + 1, NewCount);
count_factors(N, I, Count) when I * I =:= N ->
Count + 1;
count_factors(_, _, Count) ->
Count.
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Go Count Factors
func countFactors(n int) int {
count := 0
i := 1
for i*i < n {
if n%i == 0 {
count += 2
}
i++
}
if i*i == n {
count++
}
return count
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Haskell Count Factors
countFactors :: Int -> Int
countFactors n = finalize (loop 1 0)
where
loop i count
| i * i < n = loop (i + 1) (if n `mod` i == 0 then count + 2 else count)
| otherwise = (i, count)
finalize (i, count) = if i * i == n then count + 1 else count
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Java Count Factors
public class Solution {
public static int countFactors(int n) {
int count = 0;
long i = 1;
while (i * i < n) {
if (n % i == 0) {
count += 2;
}
i++;
}
if (i * i == n) {
++count;
}
return count;
}
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Lisp Count Factors
(defun count-factors (n)
(let ((count 0)
(i 1))
(loop while (< (* i i) n)
do (progn
(when (zerop (mod n i))
(incf count 2))
(incf i)))
(when (= (* i i) n)
(incf count))
count))
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
PHP Count Factors
function countFactors(int $n): int
{
$count = 0;
$i = 1;
while ($i * $i < $n) {
if ($n % $i === 0) {
$count += 2;
}
$i++;
}
if ($i * $i === $n) {
++$count;
}
return $count;
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Python Count Factors
def count_factors(n: int) -> int:
count = 0
i = 1
while i * i < n:
if n % i == 0:
count += 2
i += 1
if i * i == n:
count += 1
return count
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.