TITLE: Find formula for function of $n$ returning $0$ if $n$ is composite and $1$ if $n$ is prime
QUESTION [7 upvotes]: Sample problem: 

Find an equation $\theta(n)$ for which $\theta(n)=\left\{ \begin{array} &0, \text{when } n\in \text{Composed} \\ n, \text{when } n\in \text{Prime} \end{array} \right.$

This problem is from the International Youth Math Challenge $2018$ and since they do not return marked sheets, I am unsure if my solution was correct. 
My final answer was: $$\theta (n)=n-n\cdot \text{sgn} \left(\prod_{i=1}^{\infty} |n-p_i|\right)$$ where $p_i$ is the $i^{\text{th}}$ prime number. This is all I could come up with and to be honest, I am not too happy with it, because I feel like I have basically chosen something that will only give the answer I want. Is this solution correct, mathematically? Is there a better solution? 
Note: $\text {sgn}(n)$ is the $\text{sign}$ or $\text{signum}$ function and $$\text{sgn}(n)=\left\{ \begin{array} &-1; \ \ n\lt 0\\ \ \ \ 0; \ \ n=0\\ \ \ \ 1; \ \ n\gt 0 \end{array} \right.$$

REPLY [5 votes]: I'm not sure what kind of functions are allowed but here is a similar one (might be equivalent after some small changes), the differences being it's finite and doesn't require ability to select primes:  
For any positive integer $p$, define this function
$$
f(n,p):= \left\lceil \frac{n-p\lfloor \frac{n}{p}\rfloor}{n} \right\rceil
$$
If $p$ divides $n$ then $f(n,p)=0$, otherwise $n-p\lfloor n/p\rfloor\neq 0$ so $f(n,p)=1$.  
You can then use this to make the following:
$$
\theta(n):= n - n\prod_{p=2}^{n-1}f(n,p)
$$
If $n$ is composite then one of the $p$'s will make the product $0$ and hence $\theta(n)=n$. Otherwise $n$ is prime and the product is $1$, giving $\theta(n)=0$.