1
$\begingroup$

Suppose S is a set of (complex) numbers.

I want to know whether S contains rational whose denominator is 2.

For example,

when S={1,2,3,4,1/4}, return "False".

when S={1,2,3,1/4,-5/2}, return "True" (because of -5/2).

when S={1/3,1/4,1/5,2/7,Sqrt[2]/2,i/2}, return "False" (since both sqrt(2)/2 and i/2 are not rational).

when S={Sqrt[9/4],2/3}, return "True". (because Sqrt[9/4]=3/2).

Please give me any hint or any code.

Thank you!

$\endgroup$
2
  • 4
    $\begingroup$ AnyTrue[S, # \[Element] Rationals && Denominator[#] == 2 &] $\endgroup$ Jul 2 '20 at 6:55
  • $\begingroup$ Thank you, It works very well! $\endgroup$ Jul 2 '20 at 7:18
2
$\begingroup$

Try this:

MemberQ[S,Rational[n_Integer,2]]

For example, if

S1 = {1, 2, 3, 4, 1/4};
 MemberQ[S1,Rational[n_Integer,2]]

(*  False  *)

while if

S2 = {1, 2, 3, 1/4, -5/2};
MemberQ[S2,Rational[n_Integer,2]]

(*  True  *)

Have fun!

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