GolfScript, 22/20 (20/19) bytes
n(6?,:|2>{(.p|%-.}do:n
At the cost of speed, the code can be made two bytes shorter:
n(6?,:|2>.{|%2>-}/n*
If the output format specified in the edited question is disregarded (which is what many of the existing answers do), two bytes can be saved in the fast version and one can be saved in the slow one:
n(6?,:|2>{(.p|%-.}do
n(6?,:|2>.{|%2>-}/`
This will print an additional LF after the primes for the fast version, and it will print the primes as an array for the slow one.
How it works
Both versions are implementations of the sieve of Eratosthenes.
The fast version does the following:
Set A = [ 2 3 4 … 999,999 ]
and | = [ 0 1 2 … 999,999 ]
.
Set N = A[0]
and print N
.
Collect every N-th element from |
in C
. These are the multiples of N
.
Set A = A - C
.
If A
is non-empty, go back to 2.
n(6? # Push "\n".pop() ** 6 = 1,000,000.
,:| # Push | = [ 0 1 2 … 999,999 ].
,2> # Push A = [ 2 3 4 … 999,999 ].
{ #
( # Unshift the first element (“N”) of “A”.
.p # Print “N”.
|% # Collect every N-th element from “A” into a new array, starting with the first.
- # Take the set difference of “A” and the array from above.
. # Duplicate the set difference.
}do # If the set difference is non-empty, repeat.
:n # Store the empty string in “n”, so no final LF will get printed.
The slow version works in a similar fashion, but instead of successively removing multiples of the minimum of “A” (which is always prime), it removes multiples of all positive integers below 1,000,000.
Competitiveness
In absence of any built-in mathematical functions to factorize or check for primality, all GolfScript solutions will either be very large or very inefficient.
While still far from being efficient, I think I have achieved a decent speed-to-size ratio. At the time of its submission, this approach seems to be the shortest of those that do not use any of the aforementioned built-ins. I say seems because I have no idea how some of the answers work...
I've benchmarked all four submitted GolfScript solutions: w0lf's (trial division), my other answer (Wilson's theorem) and the two of this answer. These were the results:
Bound | Trial division | Sieve (slow) | Wilson's theorem | Sieve (fast)
----------+--------------------+--------------------+------------------+----------------
1,000 | 2.47 s | 0.06 s | 0.03 s | 0.03 s
10,000 | 246.06 s (4.1 m) | 1.49 s | 0.38 s | 0.14 s
20,000 | 1006.83 s (16.8 m) | 5.22 s | 1.41 s | 0.38 s
100,000 | ~ 7 h (estimated) | 104.65 (1.7 m) | 35.20 s | 5.82 s
1,000,000 | ~ 29 d (estimated) | 111136.97s (3.1 h) | 3695.92 s (1 h) | 418.24 s (7 m)
12
It's not an exact duplicate, but it is essentially just primality testing, which is a component of a number of existing questions (e.g. http://codegolf.stackexchange.com/questions/113, http://codegolf.stackexchange.com/questions/5087 , http://codegolf.stackexchange.com/questions/1977 ). FWIW, one guideline which isn't followed enough (even by people who should know better) is to pre-propose a question in the meta sandbox http://meta.codegolf.stackexchange.com/questions/423 for criticism and discussion of how it can be improved before people start answering it.
– Peter Taylor – 2012-05-26T08:42:47.903Ah, yes, I was worried about this question being too similar to the plethora of prime number-related questions already around. – Delan Azabani – 2012-05-26T08:44:30.843
1
A few years back I submitted an IOCCC entry that prints primes with only 68 characters in C -- unfortunately it stops well short of a million, but it might be of interest to some: http://computronium.org/ioccc.html
– Computronium – 2017-06-25T21:45:06.0631@ɐɔıʇǝɥʇuʎs How about
1e6
:-D – Titus – 2018-03-03T02:09:05.810In all those solutions that spell out "1000000", why not run to 999999 instead of 1000000, saves a byte. Everyone knows that 1000000 is not prime. – Glenn Randers-Pehrson – 2014-04-01T16:13:32.717
2@GlennRanders-Pehrson Because
10^6
is even shorter ;) – ɐɔıʇǝɥʇuʎs – 2014-05-14T05:20:28.473