3
$\begingroup$

How can I ask Mathematica to range all the natural numbers $1<n<500$ which are not multiples of 5? I mean I need to get $$\{1,2,3,4,6,7,8,9,11,12,13,14,16,...,499\}$$

$\endgroup$
  • 1
    $\begingroup$ To list two ways to do this: Array[If[FractionalPart[#/5] == 0, Nothing, #] &, 500] and Flatten[Array[{-4, -3, -2, -1} + 5*# &, 100]] both work. $\endgroup$ – N0va Oct 28 at 18:26
  • 3
    $\begingroup$ Drop[Range@500, 5 ;; ;; 5] $\endgroup$ – Michael E2 Oct 28 at 18:33
  • 1
    $\begingroup$ Pick[Range[500], Unitize@Mod[Range[500], 5], 1] $\endgroup$ – Szabolcs Oct 28 at 18:33
  • 1
    $\begingroup$ You can generalize the first option given in my earlier comment to exclude even terms as well by modifying the If statement. $\endgroup$ – N0va Oct 28 at 18:53
  • 1
    $\begingroup$ This is clearly a homework question. What have you tried so far? $\endgroup$ – yarchik Oct 28 at 19:21
1
$\begingroup$

There are very many ways to do this in Mathematica, but I assume you want something clear and able to be easily generalized to your needs. I suggest something like:

Sort[ n /. FindInstance[ {
  1 < n < 500, n != 5*m}, {n, m}, Integers], 500]]

which finds all integers n between 1 and 500 exclusive which are not multiples of 5. You can use other conditions and constraints on n.

| improve this answer | |
$\endgroup$

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