primes

List of all prime numbers up to a limit.

Category: Mathematics > Elementary Math

Syntax

p = primes(n)

Returns a row vector with every prime less than or equal to n.

Description

The primes are produced by the sieve of Eratosthenes: all integers up to n start as candidates, and the multiples of each prime found are crossed out. The method is fast, but its memory use grows with n, so very large limits are limited by available memory.

The count of primes up to n is numel(primes(n)), which grows roughly like n/log(n).

Input arguments

n
Real scalar upper limit. It need not be an integer.

Output arguments

p
Row vector of primes in increasing order. It is empty if n < 2.

Example

Primes up to 50

p = primes(50)
numel(primes(1000))
primes(1)
Output
p = 1×15

     2     3     5     7    11    13    17    19    23    29    31    37    41    43    47
ans = 168
ans = 1×0 empty double row vector

See also

isprime factor gcd nchoosek