0
$\begingroup$

I have defined the following function

g[v_] := (
   l = FactorList[v];
   Return[Product[Sum[l[[x]][[1]]^y, {y, 0,l[[x]][[2]]}], {x, 1, Length[l]}]])`

I expected g[x+1] to return 2(x+2), but it returns 8 instead. Similarly g[x+2] returns 10 instead of 2(x+3). What is wrong with the function definition?

$\endgroup$

closed as off-topic by Daniel Lichtblau, m_goldberg, MarcoB, garej, LCarvalho Jul 10 '17 at 11:37

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." – Daniel Lichtblau, m_goldberg, MarcoB, garej, LCarvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ What have you done thus far to debug it? $\endgroup$ – Daniel Lichtblau Jul 9 '17 at 20:54
  • $\begingroup$ @DanielLichtblau Not much, I really don't see where this goes wrong :( $\endgroup$ – Romain S Jul 9 '17 at 20:58
  • $\begingroup$ If you add Print[l]; right after that Product[ (so right before Sum[...]) it might become clear. This is the sort of thing one does to debug unexpected behavior. $\endgroup$ – Daniel Lichtblau Jul 9 '17 at 21:07
  • $\begingroup$ using l for variable name is really not a good idea at all. It looks like 1. $\endgroup$ – Nasser Jul 10 '17 at 4:01
  • 1
    $\begingroup$ Another debugging tip. When you get a numeric result from a function when you expect a symbolic one, try changing symbolic arguments given to the function to use variables that do not appear in its code body. For instance, if you had tried giving g the argument u +1, g would have evaluated to 2(2 + u) causing, I hope, an alarm to sound in your head. $\endgroup$ – m_goldberg Jul 10 '17 at 4:07
2
$\begingroup$

The problem is that you use a global variable as your index in the Product, and it is the same global variable x you give as an input, e.g. compute g[z+1] and you will get what you expect. To solve this issue you should localize your iterator x. For example like this, using Module:

g[v_] := Module[{x, y, l},
  l = FactorList[v];
  Product[Sum[l[[x, 1]]^y, {y, 0, l[[x, 2]]}], {x, 1, Length[l]}]
]

Here I also localized y and l, and removed the Return, as it is unnecessary.

$\endgroup$

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