1
$\begingroup$

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?

$\endgroup$

closed as off-topic by Bob Hanlon, Henrik Schumacher, m_goldberg, José Antonio Díaz Navas, J. M. is away Oct 21 '18 at 10:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, Henrik Schumacher, m_goldberg, José Antonio Díaz Navas, J. M. is away
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ I think d[x_] :=DivisorSigma[0, x] is better. You missed the ":" $\endgroup$ – J42161217 Oct 18 '18 at 14:30
  • 1
    $\begingroup$ 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. $\endgroup$ – eyorble Oct 18 '18 at 16:18

Browse other questions tagged or ask your own question.