5
$\begingroup$

I would like to format the output. For example, the following code gives

For[n=2,n<=10,n++,
  factorization =FactorInteger[n];
  length=Length[factorization];
  If[length==1,
    factorization =(Superscript@@@FactorInteger[n])[[1]],
    factorization =CenterDot@@(Superscript@@@FactorInteger[n])
  ];

  Print["n = ", n, " = ", factorization, ", number of prime factors = ", length];
]

enter image description here

But what I want is something similar to the following (but better alignment)

enter image description here

I know there is Grid, but I don't know how to use them in this current case. Also, I prefer a simpler solution (if there is), such as one can specify a "field width" like in the C language.

$\endgroup$
6
  • 2
    $\begingroup$ How about a different approach using Grid? Layout-wise, this will be much more comfortable and powerful. $\endgroup$
    – Yves Klett
    Commented Feb 14, 2014 at 10:53
  • 2
    $\begingroup$ ... hijacking your code: Reap[For[n = 2, n <= 10, n++, factorization = FactorInteger[n]; length = Length[factorization]; If[length == 1, factorization = (Superscript @@@ FactorInteger[n])[[1]], factorization = CenterDot @@ (Superscript @@@ FactorInteger[n])]; Sow[{"n = ", n, " = ", factorization, "; number of prime factors = ", length}];]][[2, 1]] // Grid $\endgroup$
    – Yves Klett
    Commented Feb 14, 2014 at 10:55
  • 1
    $\begingroup$ I really need to understand Reap/Sow :P $\endgroup$
    – Öskå
    Commented Feb 14, 2014 at 10:56
  • $\begingroup$ @YvesKlett: Great. I didn't know how to use Grid because I used Print[]. Thanks $\endgroup$
    – user565739
    Commented Feb 14, 2014 at 10:57
  • 1
    $\begingroup$ I would encourage you to change your username into something more unique, which simplifies the interaction and enhances the overall community experience :D $\endgroup$
    – Yves Klett
    Commented Feb 14, 2014 at 12:41

2 Answers 2

3
$\begingroup$

Grid is really quite useful for things like that if you can provide a finished list with results.

In this case I was to lazy to replace your procedural code (e.g. with Table) and just extracted the data you would have printed with Sow and Reap:

data = Reap[For[n = 2, n <= 10, n++, factorization = FactorInteger[n];
     length = Length[factorization];
     If[length == 1, 
      factorization = (Superscript @@@ FactorInteger[n])[[1]], 
      factorization = CenterDot @@ (Superscript @@@ FactorInteger[n])];
     Sow[{"n = ", n, " = ", factorization, 
       "; number of prime factors = ", length}];]][[2, 1]];

Grid[data]

Mathematica graphics

Now you can start playing around with Grid options to get a format of your liking.

$\endgroup$
0
5
$\begingroup$

Supposing you want to print from inside the loop (not waiting for the loop to finish) you can use Grid on each line with specified field widths:

 Do[
      Print@Grid[{{
        RandomChoice[DictionaryLookup["*"]], "=", 
        RandomReal[{1, 10}]^RandomInteger[12]}},
          ItemSize -> {{10, Full, 8}},
          Alignment -> {{Right, Center, Right}}],
    {10}]

enter image description here

Another approach for nicely formatted monitoring..

out = {};
Monitor[
     Do[  Pause[1]; 
          AppendTo[out, {RandomChoice[DictionaryLookup["*"]], "=", 
                         RandomReal[{1, 10}]^RandomInteger[12]}],
       {10}], 
                Grid[out, Alignment -> {Right, Center, Right}]]
$\endgroup$
1
  • $\begingroup$ Good point about using fixed widths anyway! $\endgroup$
    – Yves Klett
    Commented Feb 15, 2014 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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