1
$\begingroup$

Suppose $f(n)$ is a function that equals $0$ for odd inputs of $n$ and $1$ for even inputs. Note that $n$ can only be an integer. Is there a way of explicitly defining $f(n)$ so that satisfy the above conditions, without having to use a piecewise function?

$\endgroup$

6 Answers 6

10
$\begingroup$

Consider $$ f(n):=\frac{1+(-1)^n}{2}. $$

$\endgroup$
5
$\begingroup$

Another option is $$f(n) = \cos^2 \left(\frac{n\pi}{2}\right)$$

$\endgroup$
5
$\begingroup$

Let $$f(n)=\frac{1+\cos n\pi}2$$

$\endgroup$
3
$\begingroup$

Yet another one: $\;f(n) = 1 - \left\lfloor \frac{n+1}{2}\right\rfloor + \left\lfloor \frac{n}{2}\right\rfloor\,$.

$\endgroup$
3
$\begingroup$

$f(n) := 1 - \text{mod}(n, 2).$

$\endgroup$
2
$\begingroup$

$$f(n) = 1 -( n -2 \lfloor \frac{n}{2} \rfloor)$$

$\endgroup$