1
$\begingroup$

I can't transform a simple code into a function. The function should take an integer and return a list of digits in a factorial number system:

X=36177
j = 2; x = {}; While[X > 0, AppendTo[x, Mod[X, j]]; 
X = Quotient[X, j]; j++]; x 

When I use

f[X_Integer] := 
Module[{j, x}, j = 2; x = {}; 
While[X > 0, AppendTo[x, Mod[X, j]]; X = Quotient[X, j]; j++]; x]

as is recommended I get next message:

Set::setraw: Cannot assign to raw object 36177. >>

Set::setraw: Cannot assign to raw object 36177. >>

Set::setraw: Cannot assign to raw object 36177. >>

General::stop: Further output of Set::setraw will be suppressed during this calculation. >>

What do I do wrong? Please help me with this code.

$\endgroup$
4
$\begingroup$

In Mathematica you cannot modify the values of parameters to a function.

Try using local copies of the values, which you can modify, and see if that works for you.

f[X_Integer] :=Module[{j, x, xX}, j = 2; x = {}; xX = X;
  While[xX > 0, AppendTo[x, Mod[xX, j]]; xX = Quotient[xX, j]; j++]; x];
f[13]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.