# Factoring Polynomials [closed]

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?

• What have you done thus far to debug it? – Daniel Lichtblau Jul 9 '17 at 20:54
• @DanielLichtblau Not much, I really don't see where this goes wrong :( – Romain S Jul 9 '17 at 20:58
• 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. – Daniel Lichtblau Jul 9 '17 at 21:07
• using l for variable name is really not a good idea at all. It looks like 1. – Nasser Jul 10 '17 at 4:01
• 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. – m_goldberg Jul 10 '17 at 4:07

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},

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