Mathematica -- generate a list of primes up to a limit

Viewed 2604

What is the simplest function that generates a list of primes up to the argument? Its not hard to come up with such a function, for instance:

foo[n_] := Block[{A = {}, p = 2},
           While[p < n, A = Append[A, p]; p = NextPrime[p]];
           A]

However, this seems overly messy. I would like to do something like

foo[n_] := Table[Prime[i], {i,2,???}]

Where ??? is the index ofNextPrime[n,-1]. Is this possible?

3 Answers
Related