# How to quickly search and print a large list of coefficients

The following calculation is very fast.

k = 3 5 7 11 13 17; Short[Factor[x^k - 1], 5] // AbsoluteTiming However, if the specific coefficients are expressed in red as follows, the calculation speed becomes too slow.

k = 3 5 7 11 13; Factor[x^k - 1] /. Times[x_Integer, y_] :> Times[If[x > 1 || x < -1, Style[x, Red, Bold], x], y] // AbsoluteTiming


This is much slower when I try to create a CDF with Manipulate.

Manipulate[ Short[Factor[x^n - 1], 2] /. Times[x_Integer, y_] :>
Times[If[x > 1 || x < -1, Style[x, Red, Bold], x], y] // AbsoluteTiming, {n, 3 5 7 11 13 }, AppearanceElements -> None, ContinuousAction -> None ]


Q1. I want to find a way to speed up the calculation.

Q2. I have the following results expressed in Manipulate, I would like to display the screen to control the set size limit as shown above. This is the most fast version I found.

The trick is, to treat the result of "Factor" as a list.

k = 3 5 7 11*13  ; (fa13 = Factor[x^k - 1]);

(# /. (b_Integer /; b != 0 && b != 1 && b != -1)*x^a_ ->
Style[b, Red, Bold]*x^a) & /@ fa13 // AbsoluteTiming Even the conversion of factor up to 17 takes less than one second

k = 3 5 7 11*13*17  ; (fa17 = Factor[x^k - 1]);

(# /. (b_Integer /; b != 0 && b != 1 && b != -1)*x^a_ ->
Style[b, Red, Bold]*x^a) & /@ fa17 // AbsoluteTiming • Thank you for your kind reply. – user21427 Jul 3 '17 at 12:11

As we can see from the above output, coefficients and variables are reversed. Moreover, the red values are moved to the very end.

To preserve the original order:

form[m : Times[a_Integer, b_]] /; a > 1 :=
Row[{Style[a, Red], Spacer@3, b}]

form[a_] := a

fac = Factor[x^165 - 1];

Unprotect[Plus];
ClearAttributes[Plus, Orderless];

res = Map[form, fac, {2}];


An image of the last factor: Don't forget to reverse the changes to Plus:

SetAttributes[Plus, Orderless];
Protect[Plus];

• Thank you for your kind reply. – user21427 Jul 3 '17 at 12:11