Is that true that all the prime numbers are of the form $6m \pm 1$?

This is true of all prime numbers except for $2$ and $3$. The reason is that numbers with remainders $0$, $2$ and $4$ modulo $6$ are divisible by $2$, and numbers with remainders $0$ and $3$ modulo $6$ are divisible by $3$, so other than $2$ and $3$ themselves, all prime numbers must have remainder $1$ or $5$ modulo $6$.


The classes $1$ and $5$ are the two reduced residue classes modulo $6$, i.e., they are the congruence classes modulo $6$ which are relatively prime to $6$. Any sufficiently large prime number has to lie in one of these reduced residue classes, since otherwise it would share a common factor with $6$. In this case it is pretty clear that "sufficiently large" means any prime $p \geq 5$.

More generally, let $d$ be any positive integer. Then by the same reasoning all but finitely many primes must lie in one of the $\varphi(d)$ reduced residue classes modulo $d$. Since there are $d$ residue classes modulo $d$ in all, this shows that the "probability" that a large number is prime is at most $\frac{\varphi(d)}{d}$. It is not hard to show that for all $\epsilon > 0$, there exists $N \in \mathbb{Z}^+$ such that $\frac{\varphi(N)}{N} < \epsilon$. This shows that the "probability that a large number is prime" is zero -- or, when this probability business is made rigorous by recasting it in terms of density -- that the prime numbers have zero density inside the positive integers:

$\lim_{n \rightarrow \infty} \frac{ \pi(n)}{n} = 0$.

(This is a weak consequence of the Prime Number Theorem, but as we saw, one certainly need not know PNT in order to prove it.)

This argument is given in $\S 3$ of these notes from an undergraduate number theory course. The argument seems not to be entirely standard, but I think it is an appealingly direct way to show that the primes have density zero. There is also a funny irony here in that the way to see the above claim about the $\varphi$ function is to take $N = p_1 \cdots p_k$ for large $k$ (the argument is given in $\S 3$ of this other handout). Thus, in order to show that the primes have density zero, we are using the fact that there are infinitely many of them. Well, if we happened not to know that, then we would divide the proof into two cases: in case there were only finitely many primes, their density would clearly be zero!


All integers $n\ge 3$ take one of these forms

$$6k,6k\pm 1,6k\pm 2=2\left( 3k\pm 1\right) ,6k\pm 3=3(2k\pm 1),\qquad k=1,2,3,\dots$$

where $6$ is the product of the first two primes ($2$ and $3$). Since $6k,6k\pm 2,6k\pm 3$ are not primes, we are left with $p=6k\pm 1$. So, all primes $p>3$ are of the form $$6k\pm 1\qquad k=1,2,3,\dots .$$

Added: as examples (asked for in the edited question), we can take the prime number $17$, which is of the form $17=6k-1=6\cdot 3-1$ and the prime number $31$, of the form $31=6k+1=6\cdot 5+1$.


Taking the product of the first three primes $30=2\cdot 3\cdot 5$ this argument generalizes immediately to

$$p=30k\pm r,\qquad p\geq 13,$$

with $r=1,7,11,13$.

And by an easier argument we could also prove that if a positive integer is not of the form $4k\pm 1$, ($k=1,2,3,\dots$), then it is not a prime.


This generalizes the

Proposition. If a number $p>2$ is a prime, then

$$p=4k\pm 1.$$