Commodore 64 Basic, 55 characters
1F┌I=2TO10^6:F┌J=2TOI/2:IF(I/J=INT(I/I))G┌3
2N─:?I
3N─I
PETSCII substitutions: ┌
= SHIFT+O
, ─
= SHIFT+E
Incredibly slow: first, because the algorithm is extremely inefficient (it tries dividing by every value less than half the candidate number), second, because the Commodore 64 is slow, and third, because Commodore Basic does all its math in emulated floating-point on an 8-bit CPU.
Theoretical solution, 82 characters
1M=10^6:D╮S(M):F┌I=2TO1000:F┌J=I^2TOMST─I:S(J)=-1:N─:N─:F┌I=2TOM:IF(N┌S(I))T|:?I
2N─
╮
= SHIFT+I
, ┌
= SHIFT+O
, ─
= SHIFT+E
, |
= SHIFT+H
If this program could run on an actual Commodore 64, it would be much faster than the above. However, it can't: the sieve alone would take 5,000,007 bytes out of the 38,911 bytes a C64 has available for Basic programs. Note the use of -1
instead of 1
when denoting composite values in the array: C64 Basic doesn't have a true boolean negation; NOT
performs a two's complement instead.
10^6
is even shorter ;) \$\endgroup\$1e6
:-D \$\endgroup\$