# Exclude Multiples of 5 Greater than 5 from List

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.

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

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 &]]}
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}