1
$\begingroup$

So as we all know, all primes fall within the domain of 6 plus or minus 1, which I produced a list of candidates for as "c" by way of the code below.

n = Range[50];
u = 6 n - 1;
v = 6 n + 1;
c = Sort[Join[u, v]];

How could I exclude all numbers in c greater than 5 for which $c~\textrm{mod}_5 = 0$ (e.g., 25)? This should be easy!

At first I tried conditional expressions but it seemed it would not print because there was an element in the set like 25 that met the condition, so maybe what I'm looking for is something with Except, but I need 5 included.

$\endgroup$
  • $\begingroup$ 3 is a prime... how does it fall in the domain of 6 +/-1? $\endgroup$ – bill s Sep 26 '16 at 21:06
  • $\begingroup$ It doesn't, 2 and 3 being the exceptions, but they're obvious so I can include them manually $\endgroup$ – Travis Arlen McCracken Sep 26 '16 at 21:07
  • 1
    $\begingroup$ Cases[c, a_ /; Mod[a, 5] != 0 || a == 5] $\endgroup$ – march Sep 26 '16 at 21:08
  • $\begingroup$ That did the trick, thanks a bundle! $\endgroup$ – Travis Arlen McCracken Sep 26 '16 at 21:09
  • $\begingroup$ Something like Pick[c, c/5, Except[Except[1,_Integer]], {1}] might work $\endgroup$ – LLlAMnYP Sep 27 '16 at 7:31
1
$\begingroup$

One possibility:

Complement[c, Select[c, Mod[#, 5] == 0 &]]

If you need to add 5 to the list, then it can be done directly:

Flatten@{5, Complement[c, Select[c, Mod[#, 5] == 0 &]]}
$\endgroup$
1
$\begingroup$
DeleteCases[c, s_ /; Mod[s, 5] == 0 && s > 5]

or

Select[c, # == 5 || Mod[#, 5] =!= 0 &]

or

Pick[c, # == 5 || Mod[#, 5] != 0 & /@ c]

{5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 77, 79, 83, 89, 91, 97, 101, 103, 107, 109, 113, 119, 121, 127, 131, 133, 137, 139, 143, 149, 151, 157, 161, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 203, 209, 211, 217, 221, 223, 227, 229, 233, 239, 241, 247, 251, 253, 257, 259, 263, 269, 271, 277, 281, 283, 287, 289, 293, 299, 301}

$\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.