Python 3Python 3, 4645 bytes
k=P=1
while k<1e6:P%k and printP%k>0==print(k);P*=k*k;k+=1
By the time the loop reaches testing k
, it has iteratively computed the squared-factorial P=(k-1)!^2
. If k
is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1)
, so it's not a factor of P
. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4
from falsely being called prime.
More strongly, it follows from Wilson's Theorem that when k
is prime, P%k
equals 1
. Though we only need that it's nonzero here, it's useful in general that P%k
is an indicator variable for whether k
is prime.
Thanks to @Sisyphus for 1 byte with P%k>0==print(k)
using chained operator short-circuiting in place of P%k and print(k)
.