Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 532
user1339898 Avatar asked Aug 05 '12 04:08

user1339898


1 Answers

For example

f[x_] := Prime[Range@PrimePi@x]

Usage

Grid[Table[{x, f[x]}, {x, 13, 20}], Frame -> All]

Mathematica graphics

like image 173
Dr. belisarius Avatar answered Sep 29 '22 02:09

Dr. belisarius