11.3 Prime numbers

How do you construct prime numbers in Sage?

The class Primes allows for primality testing:

sage: 2^(2^12)+1 in Primes()
False
sage: 11 in Primes()
True

The usage of next_prime is self-explanatory:

sage: next_prime(2005)
      2011

The Pari command primepi is used via the Sage command pari(x).primepi(). This returns the number of primes $ \leq x$ , for example:

sage: pari(10).primepi()
      4

Using primes_first_n or primes one can check that, indeed, there are $ 4$ primes up to $ 10$ :

sage: primes_first_n(5)
[2, 3, 5, 7, 11]
sage: list(primes(1, 10))
[2, 3, 5, 7]

See About this document... for information on suggesting changes.