I want to add this answer to provide something more mathematical. I'm also going to simplify the process by using the shortcuts available in programming. Any examples I use will be Java-ish code, because it's the easiest language for me to explain it.
It is correct to say that x is an integer if the 'floor(x)' is equal to x. Although this is all you'd need in any computing environment, to prove that it is mathematical requires defining properly what a 'floor' really is:
Floor(x) = max { n ∈ Z | n ≤ x }
For anyone reading this who read that and felt light-headed, it basically means the 'floor' of a value is found by cycling through the integers ( n ∈ Z ) given that ( | ) that integer is less than or equal to x. And it's simply looking for the biggest (max) integer that satisfies this.
You can program this by starting at integer value 0, seeing if it is greater than 'x', and if not, incrementing it by 1 and checking again. When the largest integer has been found, the function's boolean is answered by if said integer is equal to x. Of course, if the value you're checking is negative you will want to change this.
Have a look at this in Java for example (a 'double' data type supports decimals; an 'int' does not):
public static int floor(double x){
int a = 0;
while( a + 1 <= x ) a++; // (a++ is the same as 'a = a + 1')
return a;
}
public static boolean isInteger(double x){
return (Class.floor(x) == x);
}
You can quote all the symbols and such when explaining the functions, and this is how you'd implement it. The method by which you get an answer isn't very efficient or useful for big numbers, but this shows at least that it can be done with the maths intact.
This isn't relevant to the Maths forum but you might be interested in it anyway. When converting a decimal data type to an integer data type, a compiler will normally 'floor' the decimal to get the integer value anyway. To simplify your code (and save a lot of time) you may want to slip this in:
public static boolean cheatMethod(double x){
return ((int)x == x);
}
Note: this is neither mathematically sound, nor likely to work in every language, nor relevant to this forum. It's there because it's a simple method in general for checking if a number is an integer.
By quoting the reasoning behind the 'floor()' function and adapting it for larger / negative numbers you can show that the function has a mathematical meaning. The cheatMethod() won't get you any brownie points but will also work in some languages.
For determining whether a given (integer) number n is even of odd, you may compute n - 2 * int(n/2); if n is even it returns 0, otherwise 1.
– mau Mar 24 '13 at 10:56