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 am new to Mathematica and I have no idea how to make a basic for loop work. I want to loop through the elements of a list: Range[45, 200]. I want to select the elements (i) for which the following conditions holds: if i mod 3 and i mod 8 and i mod 12 == 1 and i mod 5 == 0 I want to print the element. How would I achieve this? I want something like this:

for i in range(45, 201):
   if i%3==1 and i%8==1 and i%12==1 and i%5==0:
      print i

Of course in Mathematica language. Sorry if this is a dumb question but the Mathematica documentation is pretty hard to understand. Thanks.

share|improve this question
1  
"the mathematica documentation is pretty hard to understand." — Which part of the documentation for For did you try to use for your problem and where did you get stuck? –  rm -rf Feb 24 '13 at 21:59
    
Possible duplicate: mathematica.stackexchange.com/q/7924/5 –  rm -rf Feb 24 '13 at 22:00
1  
Welcome to Mathematica.SE, user6078! As s0rce correctly points out, you don't need a loop here. Successful Mathematica programming requires you to get out of the loop-oriented mode of thinking. This earlier post might be helpful. –  Verbeia Feb 24 '13 at 22:05
1  
@rm-rf - related, but none of the answers there actually talk about Select and Cases. –  Verbeia Feb 24 '13 at 22:07
    
@rm-rf - I understand the documentation, but implementing what I learned with other built-in functions is what I find hard, in this case I couldn't make the For, Range, and If functions work together. I even managed to put a little program together using pure logic, but the output was wrong. –  edferda Feb 25 '13 at 2:36

3 Answers 3

up vote 6 down vote accepted

You don't need to loop through the elements. Have a look at Select:

Select[Range[45, 200],Mod[#, 3] == Mod[#, 8] == Mod[#, 12] == 1 && Mod[#, 5] == 0 &]

(*

145

*)
share|improve this answer
    
Thanks @Verbeia –  s0rce Feb 24 '13 at 22:03
    
Hope you don't mind, @s0rce, but I just added a bit from the answer I was about to post, to make it clear that loops are not needed in this case. –  Verbeia Feb 24 '13 at 22:03
    
Thanks, I've read a little bit and it seems For loops are not appreciated much in Mathematica. From now on I will try to avoid them, it will be a problem since most programming languages rely so heavily on them. –  edferda Feb 25 '13 at 2:43
    
You might like to review this question about loops and mathematica for more discussion on this topic. mathematica.stackexchange.com/questions/7924/… –  image_doctor Feb 25 '13 at 9:53

If you only want to print the result :

  If[
   Mod[#, 3] == Mod[#, 8] == Mod[#, 12] == 1 && Mod[#, 5] == 0, 
   Print[#]
   ] & /@ Range[45, 200];

the same code without the /@ which may be mysterious for beginners :

Map[
  If[
    Mod[#, 3] == Mod[#, 8] == Mod[#, 12] == 1 && Mod[#, 5] == 0, 
    Print[#]
    ] & ,
   Range[45, 200]
  ];

"only print the result" means.. that you can' t affect the result to a variable.

If you want to affect a variable see sOrce' s answer and add variable = at the beginning of his code

share|improve this answer

Defining a selector function sF:

  sF = Mod[#, 3] == Mod[#, 8] == Mod[#, 12] == 1 && Mod[#, 5] == 0 &;

you can use any of the following functions to select the elements of Range[45,200] that satisfy the criteria coded in sF:

  Select[#, sF] &@Range[45, 200] (*as in @sOrce's answer *)
  Pick[#, sF /@ #] &[Range[45, 200]]
  Cases[#, x_ /; sF[x]] &@Range[45, 200]
  Cases[#, x_?sF] &@Range[45, 200]  (* thanks: @m_goldberg *)
  # /. (x_ /; ! sF[x] :> (## &[])) & /@ Range[45, 200] (* thanks: Mr.Wizard *)
  If[sF[#], #, ## &[]] & /@ Range[45, 200] (* a variant of @andre's answer *)
  #[[SparseArray[Boole /@ sF /@ #]["NonzeroPositions"][[1]]]] &[Range[45, 200]]
  (* 145 *)

(Related Q/A:1)

share|improve this answer
    
This is a excellent answer, but here is a minor improvement: Cases[#, x_?(sF[#] &)] & can be simplified to Cases[#, _?sF] &. –  m_goldberg Feb 25 '13 at 13:45
    
@m_goldberg, very nice, thank you; updated with your suggestion. –  kguler Feb 25 '13 at 13:54

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.