Function to Check the Parity of an Integer

379 Views Asked by At

Is there any function, f(x), that outputs whether a given integer is odd or even? For example, if x were 1237, the function would output 1; and if it were 80, it would output 2.

I know this is quite easy to accomplish in programming but when it comes to paper and pen math, I am having difficulties.

3

There are 3 best solutions below

0
On BEST ANSWER

You can use $n-2\big\lfloor \frac{n}{2} \big\rfloor$, where $\lfloor n \rfloor$ is the floor function.

Also $\frac{1-(-1)^n}{2}$ and $\sin(\frac{\pi n}{2})$.

0
On

For each $a\in\mathbb{Z}$ we define $f:\mathbb{Z}\to\{0,1\}$ by letting $f(a)$ be the remainder after dividing $a$ by $2$. This definition is possible due to the division algorithm. We have $f(odd)=1$ and $f(even)=0$.

0
On

Let's assume $n$ is integer. Your parity function can be expressed as $p \equiv n $ mod $2$, with $p = 0$ for $n$ even and $p = 1$ for $n$ odd.