Learn R Programming

pracma (version 0.7.5)

primes: Prime Numbers

Description

Generate a list of prime numbers less or equal n.

Usage

primes(n)

Arguments

n
nonnegative integer greater than 1.

Value

  • vector of integers representing prime numbers

Details

The list of prime numbers up to n is generated using the "sieve of Erasthostenes". This approach is reasonably fast, but may require a lot of main memory when n is large.

In double precision arithmetic integers are represented exactly only up to 2^53 - 1, therefore this is the maximal allowed value.

See Also

isprime, ifactor

Examples

Run this code
primes(1000)
  length(primes(1e6))  # there are 78498 prime numbers less than 1,000,000.

Run the code above in your browser using DataLab