# Integer functions $f_k(n)$ which return {0,1} depending on whether or not $k|n$

In computer programs and physics problems it is often nice to have mathematical functions that work when you want them to but sort of zero out when they don't apply. I'm thinking of how useful delta functions (both Kronecker and Dirac) seem to be in loads of different contexts.

Wikipedia's formula defining the Kronecker delta function is as follows: $$\delta_{ij} = \begin{cases} 0 & \text{if i \neq j} \\ 1 & \text{if i = j} \end{cases}$$ My question is whether there are similar functions that output one only if the input integer has the desired divisor, according to: $$f_k(n) = \begin{cases} 0 & \text{if n \not\equiv 0 (mod k)} \\ 1 & \text{if n \equiv 0 (mod k)} \end{cases}$$

I started wondering this after noticing how $(1+(-1)^n)$ can be used to nicely eliminate odd terms in binomial expansions, and started wondering if this can be effectively generalized to mod-3 or higher cases. ($f_2(n)=\cos^2(n \pi/2)$ seemed like another starting decent starting point since I only cared about its integer input/outputs)

Are there names for these types of functions? Is there a book/field that uses them and describes their properties?

For the $k=3$ case, you can construct a solution from the third roots of unity. $$f_3(n)=\frac{1}{3}\bigg(1 + \big(e^{2i\pi(1/3)}\big)^n + \big(e^{2i\pi(2/3)}\big)^n \bigg)$$ The second and third term, since they are third roots of unity, equal unity if $3|n$, which satisfies the first part of the piecewise definition that was being sought. Otherwise, the two terms are equal to $\frac{-1\pm i\sqrt{3}}{2}$, which being complex conjugates to one another means the complex parts cancel out when doing the addition. This leaves only the real parts, which being $-\frac{1}{2}$ each cancels out the lone $1$.
This generalizes to higher integers $k$ using the $k^{\mbox{th}}$ roots of unity. $$f_k(n)=\frac{1}{k} \sum_{m=0}^{k-1} \Big(e^{2i\pi(m/k)}\Big)^n$$ So that's neat. Thanks for reading.