Count the divisors of a number
Pyth, 5
l{yPQ
Uses the subsets operation on the prime factors of the input, then keeps only the unique lists of factors and returns this count.
Test Suite
Explanation
Using 25 as an example, so that the subset list isn't very long
l{yPQ ## implicit: Q = eval(input()) so Q == 25
PQ ## Prime factors of Q, giving [5, 5]
y ## All subsets, giving [[], [5], [5], [5, 5]]
{ ## Unique-fiy, giving [[], [5], [5, 5]]
l ## Length, print implicity
C++ C, 43 57 56 46 43 bytes
On Martin Büttner's suggestions :
i,c;f(n){for(i=c=n;i;n%i--&&--c);return c;}
Haskell, 28 bytes
f n=sum[0^mod n i|i<-[1..n]]
Try it online!
The trick here is to test whether a remainder is 0
using the indicator function 0^
.
0^0 = 1
0^_ = 0
This works because any positive power of 0 is 0, whereas 0^0 is combinatorially the empty product of 1.
Compare this to filtering
f n=sum[1|i<-[1..n],mod n i<1]
28 bytes
f n=sum[1|0<-mod n<$>[1..n]]
Try it online!
An alternative method using matching on a constant. The <$>
infix map
might postdate this challenge.