NAME
Math::Factor - factorise integers and calculate matching multiplications.
SYNOPSIS
use Math::Factor q/: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
see above.
FUNCTIONS
factor
Factorises numbers.
$factors = factor (\@numbers);
Each number within @numbers will be entirely factorised and its factors will be saved within the hash ref $factors, accessible by the number e.g the factors of 9 may be accessed by @{$$factors{9}}.
Ranges may be evaluated by providing 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 hash ref $factors will be multplicated against each other and results that equal the number itself, will be saved to the hash ref $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 true, 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 in a scalar context.
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 in a array context.
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()
upon request.
TAGS
:all - *()
:factor - factor(), each_factor()
:match - match(), each_match()
SEE ALSO
perl(1)
LICENSE
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Steven Schubiger