4. Find all positive integers $n$ such that every positive integer whose decimal representation consists of $n-1$ digits 1 and one digit 7 is a prime number.
(31st IMO Shortlist)
Obviously, $n=1,2$ satisfy the condition.
Below is the proof that $n \geqslant 3$ does not satisfy the condition.
Every positive integer $N$ formed by $n-1$ ones and one 7 can be expressed as
$$
N=A_{n}+6 \times 10^{k}(0 \leqslant k \leqslant n) \text {. }
$$

Here, $A_{n}=\sum_{i=0}^{n-1} 10^{i}$.
Clearly, when $3 \mid n$, $N$ is a composite number.
When $3 \nmid n$, by Fermat's Little Theorem, we have
$$
10^{6} \equiv 1(\bmod 7) \text {. }
$$

Also, $10^{3} \equiv-1(\bmod 7)$, so the order of 10 modulo 7 is 6. Thus,
$$
A_{n}=\sum_{i=0}^{s-1} 10^{i}(\bmod 7),
$$

where $s \equiv n \equiv 3,6(\bmod 6)$.
Therefore, $A_{n} \equiv r \not \equiv 0(\bmod 7)$.
Hence, when $n>6$, there must exist a $k$ such that
$$
\begin{array}{l}
6 \times 10^{k} \equiv 7-r(\bmod 7) \\
\Rightarrow N \equiv 0(\bmod 7) .
\end{array}
$$

So, $N$ is not a prime number.
When $n=4,5$,
$$
1711=29 \times 59,11711=7 \times 1673 \text {. }
$$

Therefore, $N$ is a prime number only when $n=1,2$.