Solution (source code)

= Solution

Write the <dihedral group> as $D_{2n}=\langle r,s:r^n=s^2=e,\ srs=r^{-1}\rangle$. Every reflection has order two, so an element of order three must be a rotation. Such rotations exist exactly when $3\mid n$, and then they are $r^{n/3}$ and $r^{2n/3}$. The answer is therefore
$$
\boxed{2\text{ if }3\mid n,\quad 0\text{ otherwise}}.
$$