# Problem in Counting the Number of Divisors with a Function [closed]

I want to create a function in mathematica which returns the number of divisors of a parameter x. So, I created the following function:

d[x_] = Length[Divisors[x]];

And when I evaluate:

d[1]

d[2]

d[3]

d[4]

I Obtain:

Out[1]=1

Out[2]=1

Out[3]=1

Out[4]=1

However, when I evaluate:

Length[Divisors[1]]

Length[Divisors[2]]

Length[Divisors[3]]

Length[Divisors[4]]

I obtain:

Out[1]=1

Out[2]=2

Out[3]=2

Out[4]=3

So, the function does not work, but, when I evaluate each expression individually, It works. Is there a way to patch the function to return the correct values or this is a bug?

• I think d[x_] :=DivisorSigma[0, x] is better. You missed the ":" – J42161217 Oct 18 '18 at 14:30
• Without :=, the right hand side of the set is evaluated immediately. Without further context, the Length of Divisors[x] is 1, since the head only has one argument. Use := to prevent early evaluation like that. – eyorble Oct 18 '18 at 16:18