Last chance! 50% off unlimited learning
Sale ends in
n
, resp. between
n1
and n2
.primes(n)
primes2(n1, n2) nextPrime(n)
twinPrimes(n1, n2)
n1 <= n2<="" code="">.
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. primes2
computes first all primes up to sqrt(n2)
and then
applies a refined sieve on the numbers from n1
to n2
, thereby
drastically reducing the need for storing long arrays of numbers.
nextPrime
finds the next prime number greater than n
. In
general the next prime will occur in the interval [n+1,n+log(n)]
.
twinPrimes
uses primes2
and uses diff
to find all
twin primes in the given interval.
In double precision arithmetic integers are represented exactly only up to 2^53 - 1, therefore this is the maximal allowed value.
isprime, factorize
primes(1000)
primes2(1949, 2011)
nextPrime(1e+6)
twinPrimes(1e6+1, 1e6+1001)
length(primes(1e6)) # there are 78498 prime numbers less than 1,000,000.
Run the code above in your browser using DataLab