OEIS

The On-Line Encyclopedia of Integer Sequences®

Modern Implementation for Coders and Agents
Examples: 1, 1, 2, 3, 5, 8 • A000045 • Fibonacci • author:Guglielmetti

A000040

The prime numbers.

(list)   (graph)   (refs)   (listen)   (history)   (text)   (edit)

DATA

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271

METADATA

COMMENTS

EXAMPLES

FORMULA

REFERENCES

PROGRAMS

Maple

A000040 := n->ithprime(n); [ seq(ithprime(i),i=1..100) ];
# For illustration purposes only:
isPrime := s -> is(1 = sin(Pi*GAMMA(s)/s)/sin(Pi/s)):
select(isPrime, [$2..100]); # _Peter Luschny_, Feb 23 2018

Mathematica

Prime[Range[60]]

Other Programs

[n : n in [2..500] | IsPrime(n)];
a := func< n | NthPrime(n) >;
{a(n) = if( n<1, 0, prime(n))};
/* The following functions provide asymptotic approximations, one based on the asymptotic formula cited above (slight overestimate for n > 10^8), the other one based on pi(x) ~ li(x) = Ei(log(x)) (slight underestimate): */
prime1(n)=n*(log(n)+log(log(n))-1+(log(log(n))-2)/log(n)-((log(log(n))-6)*log(log(n))+11)/log(n)^2/2)
prime2(n)=solve(X=n*log(n)/2,2*n*log(n),real(eint1(-log(X)))+n)
\\ _M. F. Hasler_, Oct 21 2013
forprime(p=2, 10^3, print1(p, ", ")) \\ _Felix Fröhlich_, Jun 30 2014
primes(10^5) \\ _Altug Alkan_, Mar 26 2018
a = sloane.A000040
a.list(58)  # _Jaap Spies_, 2007
prime_range(1, 300)  # _Zerinvary Lajos_, May 27 2009
A000040(n) := block(
if n = 1 then return(2),
return( next_prime(A000040(n-1)))
)$ /* recursive, to be replaced if possible - _R. J. Mathar_, Feb 27 2012 */
-- See also Haskell Wiki Link.
import Data.List (genericIndex)
a000040 n = genericIndex a000040_list (n - 1)
a000040_list = base ++ larger where
base = [2,3,5,7,11,13,17]
larger = p : filter prime more
prime n = all ((> 0) . mod n) $ takeWhile (\x -> x*x <= n) larger
_ : p : more = roll $ makeWheels base
roll (Wheel n rs) = [n * k + r | k <- [0..], r <- rs]
makeWheels = foldl nextSize (Wheel 1 [1])
nextSize (Wheel size bs) p = Wheel (size * p)
[r | k <- [0..p-1], b <- bs, let r = size*k+b, mod r p > 0]
data Wheel = Wheel Integer [Integer]
-- _Reinhard Zumkeller_, Apr 07 2014
A000040:=Filtered([1..10^5],IsPrime); # _Muniru A Asiru_, Sep 04 2017
from sympy import primerange
print(list(primerange(2, 272))) # _Michael S. Branicky_, Apr 30 2022

PYTHON IMPLEMENTATION

Executable sequence implementation based on goulib.sequence.Sequence:

Main definition:

A000040 = Sequence(math2.primes_gen, None, math2.is_prime, 'The prime numbers')

Detail of provided methods:

Function Expression Description
iterf math2.primes_gen Generator iterating over the infinite sequence
itemf(n) None Direct computation of $n$-th term
containf(x) math2.is_prime Boolean membership test ($x \in S$)

CROSSREFERENCES

⚙ Live Sequence Evaluation Console

Test the itemf(n) and containf(x) functions directly in this page:

Result ready. Click a button above.