0
$\begingroup$

I'd like to define a function that controls if a certain number is an integer, rational, algebraic and so far. First, I tried generating a list of those numbers:

ZL = {1, 3, Pi, E, Sqrt[2], Zeta[3]} 

I then created my functions like so:

TestI[x_] := If[x ∈ Integers, x "is Integer", x "is no Integer"]

It works so far.

I now tried to define a For-function like this:

For[i = 1, i <= Length[ZL], i++, Print[TestZ[Part[ZL, i]]]]

However, when I evaluate the above expression, the output is like

is integer
3 is integer
...
is no integer E 

How can I achieve an output that says:

1 is Integer
3 is Integer
...
\e is no Integer

and so on?

$\endgroup$
  • $\begingroup$ TestI[x_] := If[x \[Element] Integers, ToString[x] <> " is Integer", ToString[x] <> " is no Integer"] Also, BAD idea to use uppercase for your symbol initials - might clash with built-in symbols. $\endgroup$ – ciao Apr 10 '14 at 9:40
  • $\begingroup$ zl = {1, 3, Pi, E, Sqrt[2], Zeta[3]}; testi[x_] := If[IntegerQ[x], x "is Integer", x "is not Integer"]; For[i = 1, i <= Length[zl], i++, Print[testi[Part[zl, i]]]] $\endgroup$ – martin Apr 10 '14 at 9:40
  • $\begingroup$ or If[Element[x , Integers] $\endgroup$ – martin Apr 10 '14 at 9:49
  • $\begingroup$ @martin Your hint wont work. I get the same results. $\endgroup$ – K. L. Apr 10 '14 at 10:18
  • $\begingroup$ @K.L. If you copy & paste entire first comment what do you get? $\endgroup$ – martin Apr 10 '14 at 10:20
5
$\begingroup$

You could achieve without If, e.g.:

f[x_Integer] := StringForm["`` is an integer", x];
f[x_] := StringForm["`` is not an integer", x];

Test:

test = {1, 3, Pi, E, Sqrt[2], Zeta[3], Zeta[-2]}

Mapping:

Column[f /@ test]

yields:

enter image description here

Please note IntegerQ[3] is True, however IntegerQ[3.] is False

$\endgroup$
  • 1
    $\begingroup$ Please consider Listable too :) $\endgroup$ – Kuba Apr 10 '14 at 9:53

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.