This answer doesn't use any number-theoretic cleverness. It spams Python's bitwise operators to create a manual "for loop", checking all pairs to see whether .
Python 2, way too many bytes (278 thanks to Jo King in the comments!)
Try it online!
This is a lot more bytes than the other answers, so I'm leaving it ungolfed for now. The code snippet below contains functions and variable assignment for clarity, but substitution turns isPrime(n) into a single Python expression.
def count(k, spacing):
return 2**(spacing*(k+1))/(2**spacing - 1)**2
def ones(k, spacing):
return 2**(spacing*k)/(2**spacing - 1)
x = count(n-1, n)
y = count(n-1, n**2)
onebits = ones(n-1, n) * ones(n-1, n**2)
comparison = n*onebits
difference = (x*y) ^ (comparison)
differenceMinusOne = difference - onebits
checkbits = onebits*(2**(n-1))
return (differenceMinusOne & checkbits == 0 and n>1)or 1<n<6 and n!=4
Why does it work?
I'll do the same algorithm here in base 10 instead of binary. Look at this neat fraction:
If we put a large power of 10 in the numerator and use Python's floor division, this gives an enumeration of numbers. For example, with floor division, enumerating the numbers .
Let's say we multiply two numbers like this, with different spacings of zeroes. I'll place commas suggestively in the product.
The product enumerates, in three-digit sequences, the multiplication table up to 4 times 4. If we want to check whether the number 5 is prime, we just have to check whether appears anywhere in that product.
To do that, we XOR the above product by the number , and then subtract the number . Call the result . If appeared in the multiplication table enumeration, it will cause the subtraction to carry over and put in the corresponding place in .
To test for this overflow, we compute an AND of and the number . The result is zero if and only if 5 is prime.