4
$\begingroup$

I am trying to write a condition clause for a certain expression. I need to pick out the following cases:

Mod[x*y, 10] == 8, or

Mod[x*y, 10] == 3, or

Mod[x*y, 10] == 7.

I need to repeat this three times, and both x and y are actually complex expressions, so it would be nice to be able to write this in a compact way.

I have tried

Mod[x*y, 10] == Or[8,3,7]

but this doesn't seem to work.

$\endgroup$
7
$\begingroup$

You can use any of the following:

MatchQ[Mod[x y, 10], 8 | 3 | 7]
MemberQ[{8, 3, 7}, Mod[x y, 10]]
AnyTrue[{8, 3, 7}, Mod[x y, 10] == # &]
Or @@ (Mod[x y, 10] == # & /@ {8, 3, 7})
$\endgroup$
2
$\begingroup$

Here's another method that's quite close to what you tried at first:

Thread[Mod[x*y, 10] == Or[8, 3, 7], Or]

Note, however, that this won't work if any of the arguments of Or are boolean since this method relies on Or not evaluating.

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