2

I'just starting with Mathematica and therefore I was having a small question.

It must be somewhere on Internet but I can't find it.

My question is the following.

I would like to get my values after having computed a Print[...] in a list.

I wanted to print the prime numbers <5

For[i=1,i<5,i++,
Print[Prime[i]];
]
Out:
2
3
5
7

But I would like to get these 4 values such as: {2,3,5,7} How do I compute that? I already tried Print[List[Prime[i]] but that gave me {2} {3} {5} {7}. Thank you!

And if I want to Print the prime values below a certain upperbound, let's say 10.

For[i=1,Prime[i]<10,i++,
Print[Prime[i]; ] 
Out: 
2
3
5 
7

And I would like to get these values in a list as well, how do I do that? I was thinking about:

Prime/@range@[length[List[Prime[i]<10]]]
Noah1997
  • 31
  • 1
  • 3

2 Answers2

7

Jason has the correct, idiomatic way of approaching this, so I am adding this for completeness as there is a way to capture Print output. But, you should not use this unless you have strong reasons to do so. What you can do is to use a handler function to capture the Print output. Handlers are executed at specific events, e.g.

In[40]:= Internal`Handlers[]

Out[40]= {"VetoableValueChange" -> {JLink`Private`jlinkVetoFunction}, 
 "ValueTrackTrigger" -> {}, "ValueChange" -> {}, "RemoveSymbol" -> {},
  "NewSymbol" -> {}, "GetFileEvent" -> {}, 
 "MessageTextFilter" -> {Internal`MessageButtonHandler}, 
 "Message" -> {}, "Wolfram.System.Print" -> {}, 
 "SystemAssertions" -> {}, "Assertions" -> {}, 
 "ProcessDebugTags" -> {}, "RuntimeToolsActive" -> {}, 
 "BreakPoint" -> {}, "GetSymbol" -> {}, "SetSymbol" -> {}}

and most are not active except under specific circumstances (like debug mode). But, "Message", "MessageTextFilter", "Wolfram.System.Print" are active all the time.

To set up a handler, there are several functions: Internal`HandlerBlock, Internal`AddHandler, and Internal`RemoveHandler. In this case, it is best to use an Internal`HandlerBlock as it automatically removes the handler when you are done using it, e.g.

Block[{out = {}},
 Internal`HandlerBlock[
  {"Wolfram.System.Print", (out = {out, #}) &},
   For[i = 1, i < 5, i++, Print[Prime[i]];]
  ];
 Flatten@out
]

A word of caution, do not use Print inside the "Wolfram.System.Print" handler as an inescapable infinite loop is entered and you have to forcibly kill the kernel.

rcollyer
  • 33,976
  • 7
  • 92
  • 191
6

All Print does is print the numbers in the output line, in this case it is not necessary at all.

There are a few ways of getting the list you are looking for. The most straightforward, and useful for learning how Mathematica does things, is

Table[Prime[i], {i, 1, 4, 1}]
(* {2, 3, 5, 7} *)

Once you figure out what that means, you can use shorthand to get it quicker

Prime@Range@4
(* {2, 3, 5, 7} *)

If you want to select only those primes less than a certain number, one direct way to do that is to get a list of primes that is certainly larger than that, and use Select to get only the ones you want,

Select[Prime@Range@20, (# < 10 &)]
(* {2, 3, 5, 7} *)

But the better way, in that it doesn't do more work than is needed, is to follow this post

Prime@Range@PrimePi@10
(* {2, 3, 5, 7} *)
Jason B.
  • 68,381
  • 3
  • 139
  • 286