Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
How about a different approach using Grid? Layout-wise, this will be much more comfortable and powerful. –  Yves Klett Feb 14 at 10:53
2  
... 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 –  Yves Klett Feb 14 at 10:55
1  
I really need to understand Reap/Sow :P –  Öskå Feb 14 at 10:56
    
@YvesKlett: Great. I didn't know how to use Grid because I used Print[]. Thanks –  user565739 Feb 14 at 10:57
1  
I would encourage you to change your username into something more unique, which simplifies the interaction and enhances the overall community experience :D –  Yves Klett Feb 14 at 12:41

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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}]]
share|improve this answer
    
Good point about using fixed widths anyway! –  Yves Klett Feb 15 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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