The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


Math::NumSeq::Primes -- prime numbers


 use Math::NumSeq::Primes;
 my $seq = Math::NumSeq::Primes->new;
 my ($i, $value) = $seq->next;


The prime numbers, not divisible by anything except themselves and 1.

    2, 3, 5, 7, 11, 13, 17, 19, ...         (A000040)
    starting i=1

Currently this is implemented with Math::Prime::XS generating blocks of primes with a sieve of Eratosthenes. The result is reasonably progressive. On a 32-bit system there's a hard limit at 2^31 (though even approaching that takes a long time to calculate).


See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

$seq = Math::NumSeq::Primes->new ()

Create and return a new sequence object.

$bool = $seq->pred($value)

Return true if $value is a prime.

In the current code a hard limit of 2**32 is placed on the $value to be checked, in the interests of not going into a near-infinite loop.

$i = $seq->value_to_i_estimate($value)

Return an estimate of the i corresponding to $value. $value can be any size, it's not limited as in pred(). See "Value to i Estimate" below.


Value to i Estimate

In the current code the number of count of primes up to value is estimated by the well-known asymptotic

    i ~= value/log(value)


Math::NumSeq, Math::NumSeq::TwinPrimes, Math::NumSeq::SophieGermainPrimes, Math::NumSeq::Emirps

Math::Prime::XS, Math::Prime::TiedArray, Math::Prime::FastSieve, Math::Prime::Util



Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde

Math-NumSeq is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-NumSeq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-NumSeq. If not, see <>.