-1
$\begingroup$

I would like to produce elements (preferably by Table or For-If commands) that they belong to specific sets. For example I want to write positive integers from 1 to 100 such that they are $2 \pmod 3$, $3,\, 5$ or $6 \pmod 7$ or $7,\,8$ or $10 \pmod {11}$.

I wrote the following code but it was not correct.

A1={2};
A2={3,5,6};
A3={7,8,10};

Union[Flatten[Table[If[(Element[Mod[i,3],A1]
||Element[Mod[i,7],A2]||Element[Mod[i, 11], A3]), i, 0], {i, 1, 100}]]]
$\endgroup$
4
$\begingroup$

You have a series of conditions you want to be true, so use Select,

A1 = {2};
A2 = {3, 5, 6};
A3 = {7, 8, 10}; Select[Range[100], 
 MemberQ[A1, Mod[#, 3]] || MemberQ[A2, Mod[#, 7]] || 
   MemberQ[A3, Mod[#, 11]] &]

(* {2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, \
23, 24, 26, 27, 29, 30, 31, 32, 33, 34, 35, 38, 40, 41, 43, 44, 45, \
47, 48, 50, 51, 52, 53, 54, 55, 56, 59, 61, 62, 63, 65, 66, 68, 69, \
71, 73, 74, 75, 76, 77, 80, 82, 83, 84, 85, 86, 87, 89, 90, 92, 94, \
95, 96, 97, 98} *)
$\endgroup$
  • $\begingroup$ You know, if you keep this up, it will force me one step closer to being off the first page of users. Thanks. :) $\endgroup$ – rcollyer Apr 15 '16 at 19:09
  • 2
    $\begingroup$ @rcollyer Once I start at alpha in June I'm sure I won't be on here as much lol $\endgroup$ – Jason B. Apr 15 '16 at 20:06
  • $\begingroup$ I'm "vaguely" familiar with that. $\endgroup$ – rcollyer Apr 15 '16 at 20:11
2
$\begingroup$

Using MemberQ I wrote the following code and it worked:

A1={2};
A2={3,5,6};
A3={7,8,10}
Union[Flatten[Table[If[(MemberQ[A1,Mod[i,3]]==True
||MemberQ[A2,Mod[i,7]]==True||MemberQ[A3,Mod[i,11]]==True),i,0], 
{i,1,100}]]]
$\endgroup$
  • $\begingroup$ Here you have Mod[i,1] which is just 0 for every number. Also, your result has a 0, but you only want numbers from 1 to 100 $\endgroup$ – Jason B. Apr 15 '16 at 13:34
  • $\begingroup$ @JasonB, thanks for comment, it was 11 not 1, I corrected it. $\endgroup$ – asad Apr 15 '16 at 17:14
  • $\begingroup$ you don't need MemberQ[...]==True; just MemberQ[...] works fine. $\endgroup$ – kglr Apr 21 '16 at 0:31
0
$\begingroup$
s1 = Union@Flatten[Position[Mod[Range[100], #], Alternatives @@ #2] & @@@ 
        {{3, A1}, {7, A2}, {11, A3}}];

s2 = Select[Range[100], MemberQ[A1, Mod[#, 3]] || MemberQ[A2, Mod[#, 7]] || 
 MemberQ[A3, Mod[#, 11]] &]; (* from Jason's answer *)

s1 == s2

True

Although speed is not the issue in this Q/A, surprisingly, for large lists, this is faster than the approach using Select or Pick:

Union@Flatten[Position[Mod[Range[100000], #], Alternatives @@ #2] & @@@
      {{3, A1}, {7, A2}, {11, A3}}, 2]; // AbsoluteTiming // First

0.128634

Pick[Range[100000],  MemberQ[A1, Mod[#, 3]] || MemberQ[A2, Mod[#, 7]] || 
       MemberQ[A3, Mod[#, 11]] & /@ Range[100000]]; // AbsoluteTiming // First

0.326279

Select[Range[100000], MemberQ[A1, Mod[#, 3]] || MemberQ[A2, Mod[#, 7]] || 
      MemberQ[A3, Mod[#, 11]] &]; // AbsoluteTiming // First

0.312955

And corrected version of OP's Table approach is somewhere in between:

Table[If[MemberQ[A1, Mod[i, 3]] || MemberQ[A2, Mod[i, 7]] || 
        MemberQ[A3, Mod[i, 11]], i, ## &[]], {i, 1, 100000}]; // AbsoluteTiming // First

0.288671

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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