Primes and Twin Primes Your challenge is to write a program to print all the primes (separated by one or more whitespace characters) less than a given integer N with an asterisk (*) next to each twin prime. A twin prime is a prime number that is either two more or two less than another prime.
(Hardcoding outputs is allowed, but it is not likely to result in very short code.)
Shortest code wins!
Input

N is always one of 50, 100, 1000, 1000000, or 10000000

Output

The output for input 50 should be 2 3* 5* 7* 11* 13* 17* 19* 23 29* 31* 37 41* 43* 47
Python 2, 81 79 bytes
-2 bytes thanks to dingledooper with a different way to detect if k-2 is a prime which works with k=2.
Uses Wilson's theorem for the primality tests and the fact that \$p\$, \$p+1\$ and \$p+2\$ are coprime for most primes \$p\$.

P=k=q=1
exec&quot;if P%k:print`k`+'*'[k&lt;3:P%(k+2)|q/k];q=k+2\nP*=k*k;k+=1\n&quot;*input()

Try it online!
The output includes the input if it is prime, but this is fine since all testcases are composite.
How?
We use the following result of Wilson's theorem:
$$
(k-1)!^2 \operatorname{mod} k = 
\begin{cases}
1 &amp; \text{if $k$ is prime}\\
0 &amp; \text{otherwise.}
\end{cases}
$$
In the code P is used to keep track of \$(k-1)!^2\$ and P%k tests if k is a prime.
If this is the case, we print the output and update q to k+2.
If k==q at the print statement, we know that k-2 was a prime and k is a twin prime.
P%(k+2) tests if k+2 is a prime number. If we would use the exact same prime test as before, this would be P*k*k*(k+1)*(k+1)%(k+2), but with our assumption that \$k\$, \$k+1\$ and \$k+2\$ are coprime for prime \$k\$, this gives the same result.
The assumption doesn't hold for k=2, but this is handled separately with '*'[k&lt;3:], which results in the empty string if k&lt;3.