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.

  • 2
    $\begingroup$ May you share why IntegerQ is not sufficient for your problem. Might be an XY problem. $\endgroup$
    – Edmund
    Jan 5, 2019 at 10:53
  • $\begingroup$ 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. $\endgroup$
    – GhoshP
    Jan 5, 2019 at 11:10
  • $\begingroup$ @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. RandomProcesses`TemporalDataDump`iIntegerQ. $\endgroup$ Jan 5, 2019 at 11:21
  • 1
    $\begingroup$ The immediate issue as to why no output: it is suppressed by the final semicolon. $\endgroup$ Jan 5, 2019 at 16:22
  • 1
    $\begingroup$ @HenrikSchumacher IntegerQ actually checks if something is internally held as an integer (I think). For example, this returns False: IntegerQ@Integer[f]. $\endgroup$
    – b3m2a1
    Jan 5, 2019 at 21:21

3 Answers 3


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 RandomProcesses`TemporalDataDump`iIntegerQ:

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


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.

  • 1
    $\begingroup$ Sign[n] n is just Abs[n], of course. ;) $\endgroup$ Jan 5, 2019 at 14:03
  • $\begingroup$ Ha, ha. Good point @J.M.! $\endgroup$ 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]]
  • 4
    $\begingroup$ 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. $\endgroup$
    – Lukas Lang
    Jan 5, 2019 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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