How to factorize $x+y$ in this kind of example?

Consider the following simple polynomial in two variables 5 + 2x + 2y. Suppose I want to simplify it to 5+2(x+y). I have no idea how Mathematica can do that. Both Simplify and FullSimplify don't work. I tried also to use Factor and FactorTerms but it also doesn't work.

To give some context, the reason I'm interest in this is that I'm simplifying some complicated expressions and in them often things of the schematic form a + b x + b y  appear, and I need to be able to factor this as a+b(x+y)because x+y can be simplified.

• Similar question but also without answer mathematica.stackexchange.com/questions/291944/… Nov 24, 2023 at 23:16
• That's interesting, but in that case FullSimplify seems to solve the issue, but here for some reason it does not. Also, I just checked and the LeafCount of 2x+2y+5 is 8 while that of 2(x+y)+5 is 7, so FullSimplify in principle should do this factorization, no? Nov 24, 2023 at 23:25
• A funny example exp = 5 + 10^10 Total@ToExpression@Alphabet[]; exp // Expand // FullSimplify Nov 24, 2023 at 23:25
• FullSimplify[5+2(x+y)] does not change anything, too, irrespective to the settings of ComplexityFunction Nov 24, 2023 at 23:40

A workaround is to replace the number with a variable of a name that you do not use elsewhere.

FullSimplify[5 + 2 x + 2 y /. (#)] /. Reverse@# &@(2 -> fu)

exp = 5 + 10^10 Total@ToExpression@Alphabet[] // Expand
FullSimplify[exp /. (#)] /. Reverse@# &@(10^10 -> fu)


5 + 2 (x + y)

5 + 10000000000 a + 10000000000 b + 10000000000 c +
10000000000 d + 10000000000 e + 10000000000 f + 10000000000 g +
10000000000 h + 10000000000 i + 10000000000 j + 10000000000 k +
10000000000 l + 10000000000 m + 10000000000 n + 10000000000 o +
10000000000 p + 10000000000 q + 10000000000 r + 10000000000 s +
10000000000 t + 10000000000 u + 10000000000 v + 10000000000 w +
10000000000 x + 10000000000 y + 10000000000 z

5 +
10000000000 (a + b + c + d + e + f + g + h + i + j + k + l + m + n +
o + p + q + r + s + t + u + v + w + x + y + z)

xfactor = (GatherBy[List @@ #, NumericQ] // Map[Apply[Head@#]] //

expr = 5 + 2 x + 2 y;
xfactor@expr

expr = a + b x + 5 + c y + 6 x + d y;
xfactor@expr

expr = 5 + 10000000000 a + 10000000000 b + 10000000000 c +
10000000000 d + 10000000000 e + 10000000000 f + 10000000000 g +
10000000000 h + 10000000000 i + 10000000000 j + 10000000000 k +
10000000000 l + 10000000000 m + 10000000000 n + 10000000000 o +
10000000000 p + 10000000000 q + 10000000000 r + 10000000000 s +
10000000000 t + 20000000000 u + 20000000000 v + 10000000000 w +
10000000000 x + 10000000000 y + 10000000000 z
xfactor@expr

• (+1) Nice, @Syed! :-) Nov 25, 2023 at 3:31

Using the third argument of GroupBy:

xfactor = Total@Values[GroupBy[List @@ #, NumericQ, Simplify@Total@# &]] &;
expr = 5 + 2 x + 2 y;

xfactor@expr

5 + 2 (x + y)

expr = 5 + 10000000000 a + 10000000000 b + 10000000000 c +
10000000000 d + 10000000000 e + 10000000000 f + 10000000000 g +
10000000000 h + 10000000000 i + 10000000000 j + 10000000000 k +
10000000000 l + 10000000000 m + 10000000000 n + 10000000000 o +
10000000000 p + 10000000000 q + 10000000000 r + 10000000000 s +
10000000000 t + 20000000000 u + 20000000000 v + 10000000000 w +
10000000000 x + 10000000000 y + 10000000000 z;

xfactor@expr

5 + 10000000000 (a + b + c + d + e + f + g + h + i + j + k + l + m +
n + o + p + q + r + s + t + 2 u + 2 v + w + x + y + z)