NAME
Math::Factor - Factorise numbers and calculate matching multiplications
SYNOPSIS
use Math::Factor ':all';
@numbers = qw(9 30107);
# data manipulation
$factors = factor( @numbers );
$matches = match( $factors );
# factors iteration
while ($factor = each_factor( $numbers[0], $factors )) {
print "$factor\n";
}
# matches iteration
while (@match = each_match( $numbers[0], $matches )) {
print "$numbers[0] == $match[0] * $match[1]\n";
}
DESCRIPTION
Math::Factor factorises numbers by applying trial divison.
FUNCTIONS
factor
Factorises numbers.
$factors = factor( @numbers );
Each number within @numbers will be entirely factorised and its factors will be saved within the hashref $factors, accessible by the number e.g the factors of 9 may be accessed by @{$factors->{9}}.
Ranges may be evaluated by supplying a two-dimensional array.
@numbers = (
[ 9, '1-6' ],
[ 1032, '1-$' ],
[ 30107, '*' ],
);
The first item (9) represents the number itself. The second item (1-6) represents the range that is being evaluated; ranges are indicated by a starting and ending value separated by a colon. $ indicates the number. Evaluating ranges for certain numbers may be entirely disabled by supplying *. 1-$ is equivalent to *.
match
Evaluates matching multiplications.
$matches = match( $factors );
The factors of each number within the hashref $factors will be multplicated against each other and results that equal the number itself, will be saved to the hashref $matches. The matches are accessible through the according numbers e.g. the first two numbers that matched 9, may be accessed by $matches->{9}[0][0] and $matches->{9}[0][1], the second ones by $matches->{9}[1][0] and $matches->{9}[1][1], and so on.
If $Math::Factor::Skip_multiple is set to a true value, matching multiplications that contain multiplicated (small) factors will be dropped.
Example:
# accepted
30107 == 11 * 2737
# dropped
30107 == 77 * 391
each_factor
Returns each factor of a number as string.
while ($factor = each_factor( $number, $factors )) {
print "$factor\n";
}
If not all factors are being evaluated by each_factor(), it is recommended to undef @{"Math::Factor::each_factor_$number"} after usage of each_factor().
each_match
Returns each match of a number as string.
while (@match = each_match( $number, $matches )) {
print "$number == $match[0] * $match[1]\n";
}
If not all matches are being evaluated by each_match(), it is recommended to undef @{"Math::Factor::each_match_$number"} after usage of each_match().
EXPORT
factor(), match(), each_factor(), each_match()
are exportable.
TAGS
:all - *()