++ed by:

2 PAUSE users

Kevin Ryde
and 1 contributors


Math::NumSeq::AlmostPrimes -- semiprimes and other fixed number of prime factors


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


This sequence is various "almost prime" numbers. These are numbers with a given number of prime factors. The default is 2 prime factors, which are the semi-primes. For example 15 because 15=3*5.

    4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, ...
    # starting i=1

Factor Count

factor_count => $c controls how many prime factors are to be used. 1 would be the primes themselves (the same as Math::NumSeq::Primes). Or for example factor count 4 is as follows. 60 is present because 60=2*2*3*5 has precisely 4 prime factors.

    # factor_count => 4
    16, 24, 36, 40, 54, 60, ...

The first number in the sequence is 2^factor_count, being prime factor 2 repeated factor_count many times.


multiplicity => 'distinct' asks for products of distinct primes. For the default factor count 2 this means exclude squares like 4=2*2, which leaves

    # multiplicity => 'distinct'
    6, 10, 14, 15, 21, ...

For other factor counts, multiplicity "distinct" eliminates any numbers with repeated factors, leaving only square-free numbers. For example factor count 4 becomes

    # factor_count => 4, multiplicity => 'distinct'
    210, 330, 390, 462, 510, 546, ...

For multiplicity "distinct" the first value in the sequence is a primorial (see Math::NumSeq::Primorials), being the first factor_count many primes multipled together. For example 210 above is primorial 2*3*5*7.


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

$seq = Math::NumSeq::AlmostPrimes->new ()
$seq = Math::NumSeq::AlmostPrimes->new (factor_count => $integer, multiplicity => $str)

Create and return a new sequence object. multiplicity can be

    "repeated"  repeated primes allowed (the default)
    "distinct"  all primes must be distinct
$bool = $seq->pred($value)

Return true if $value is an almost-prime, ie. it has exactly factor_count many prime factors, and if distinct is true then all those factors different.

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


Math::NumSeq, Math::NumSeq::Primes, Math::NumSeq::PrimeFactorCount





Copyright 2010, 2011, 2012, 2013, 2014 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 <http://www.gnu.org/licenses/>.