Which cardinals $\lambda > 2$ have the following property?
There is a space $(X,\tau)$ such that
- for all cardinals $\kappa$ with $1<\kappa<\lambda$ we have $X\not\cong X^\kappa$, and
- $X\cong X^\lambda$.
Which cardinals $\lambda > 2$ have the following property?
There is a space $(X,\tau)$ such that
As was conjectured by Adam in his answer, every finite $n > 2$ has the property you're looking for. Namely, there exists a topological space homeomorphic to its $n$th power and none of its $k$th powers, $1<k<n$.
The relevant paper is "Homeomorphisms between finite powers of topological spaces" by Orsatti and Rodino. It can be found here. From the abstract:
"Let $\lambda$ be an infinite cardinal number. It is proved that, for each positive integer $r$, there exists a compact connected homogeneous topological space $X$ of weight $\lambda$ such that $X^n$ is homeomorphic to $X^m$ iff $n \equiv m$ (mod $r$)."
From glancing at the paper, the authors adapt a big theorem of Corner, that every countable reduced torsion-free ring is the endomorphism ring of some torsion-free Abelian group, to prove their result. (This is the same theorem Corner himself uses to prove the result cited in Adam's answer.) What they actually produce is a compact and connected topological Abelian group $X$ such that $X^n$ is topologically isomorphic $X^m$ iff $n \equiv m$ (mod $r$), and then use the fact that for such groups any homeomorphism between the underlying spaces must actually be a topological isomorphism.
This is not an answer but it became too long for a comment. I would expect that every finite $\lambda>2$ has the property.
By Corner, for every finite $\lambda>2$ there exists an abelian group $G$ such that $G\cong G^\lambda$ but $G\ncong G^k$ for every $1<k<\lambda$. See a discussion here: https://mathoverflow.net/a/10194/16678.
Then by Trnková [1], every such group $G$ can be represented as $G\cong{\rm maps}(X,X)$ where $X$ is a metric space and ${\rm maps}$ is the set of non-constant maps. The group operation is the composition. Actually the result of Trnková is way more general, but this is what we might need.
I hoped that I would deduce an answer to your question from these results ans some general nonsense, but it is not so immediate we have ${\rm maps(X,X)}\cong{\rm maps}(X,X)^\lambda\subsetneq{\rm maps}(X,X^\lambda)$. We lack a bijection on the right side since there exist non-constant maps into product that are constant on some axes. If you want to find an answer to your question you may try to look into these two papers and put the Corner ideas into the context of Trnková's construction hoping they could work together :)
[1] V. Trnková, Non-constant continuous mappings of metric or compact Hausdorff spaces, Comment. Math. Univ. Carolinae 13 (1972) 283–295.