# Creating a function that checks whether input is an integer (in the mathematical sense)

I am creating an alternative function of IntegerQ[] function. Now I don't want any output. I just want my function to check whether this is integer or not. I'm gonna use it in another code. My code looks like;

IntQ[n_] := Block[{}, GP = 0;
{j = 0;
If[n >= 0,
For[i = n, i > 0, i--, j++],
For[i = (-n), i > 0, i--, j++]]};
{If[j == n, True, False]};
]


It is not working. I just want to know where is the problem, why the problem is causing and how to solve it? Generally IntegerQ[6] would return True as an output and in other function it wouldn't return anything but we can use that value actually. So that's where my problem is.

• May you share why IntegerQ is not sufficient for your problem. Might be an XY problem. Jan 5, 2019 at 10:53
• Well I was asked to create a code without using built-in functions (except for the loops or block codes). In my code I needed to use the IntegerQ[] which is not in the condition. That's why was trying to create an alternate. Jan 5, 2019 at 11:10
• @Edmund IntegerQ tests only whether the Head of the expression equals Integer. There are other undocumented functions though that attempt to check an arbitrary number (e.g. finite precision numbers) for being integers, e.g. RandomProcessesTemporalDataDumpiIntegerQ. Jan 5, 2019 at 11:21
• The immediate issue as to why no output: it is suppressed by the final semicolon. Jan 5, 2019 at 16:22
• @HenrikSchumacher IntegerQ actually checks if something is internally held as an integer (I think). For example, this returns False: IntegerQ@Integer[f]. Jan 5, 2019 at 21:21

You can define it recursively, without any loops:

intQ[0] := True
intQ[n_] := intQ[n - 1] /; n >= 1
intQ[n_] := intQ[n + 1] /; n <= -1
intQ[_] := False


This one follows the idea of your implementation and it seems to work:

IntQ[n_] := Block[{i},
i = Abs[N[n]];
While[Positive[i], i--];
i == 0.
]


Testing against the undocumented function RandomProcessesTemporalDataDumpiIntegerQ:

test = {-11.1, -1, -2., -0., 0, 1., 1, 1.5, 1, 11., 11.5, E, Pi};
IntQ /@ test == RandomProcessesTemporalDataDumpiIntegerQ /@ test


True

One reason why your function did not return anything is the final semicolon ;. Moreover, the use of braces {} for structuring your code is somewhat odd: Braces in Mathematica are used to define lists. Use parenthesis () for code structuring if necessary.

Also, your code cannot compute the correct result for negativ n, because at the end of your function, j will be always non-negative, so it cannot equal n if the latter is negative.

Other possible variants are

IntQ2[n_] := N[Round[N[n]]] == N[n]
IntQ3[n_] := N[Round[N[n]]] === N[n]


Please see the details of Equal (==) and SameQ (===) for understanding that these two tests are preformed with different tolerances.

• Sign[n] n is just Abs[n], of course. ;) Jan 5, 2019 at 14:03
• Ha, ha. Good point @J.M.! Jan 5, 2019 at 14:56

Regarding your code, try with those modifications

IntQ[n_] := Block[{},
j = 0;
If[n >= 0, For[i = n, i > 0, i--, j++],
For[i = (-n), i > 0, i--, j++]];
If[j == Abs[n], True, False]]

• Statements of the form If[cond,True,False] can usually be replaced by cond, or TrueQ@cond, if you're worried about the condition being neither True nor False. Jan 5, 2019 at 12:17