Math::Factor::XS(3)
π Math::Factor::XS π
okay bestie, so this is Math::Factor::XS β a User Contributed Perl Documentation page, and it is literally the girl who factorizes numbers for fun at parties. She takes a number, does trial divisions on it (the way that she just keeps dividing, no cap), and hands you back its factors and prime factors. math slay β¨
πΈ SYNOPSIS πΈ
here's the whole vibe, copy-paste ready:
use Math::Factor::XS ':all';
# or
use Math::Factor::XS qw(factors prime_factors matches);
$number = 30107;
@factors = factors($number);
@primes = prime_factors($number);
@matches = matches($number, \@factors);
print "$factors[1]\n";
print "$number == $matches[0][0] * $matches[0][1]\n";
like, import everything with :all, or just grab the three besties factors, prime_factors and matches. the $number is 30107 here because apparently that's the moment. π
π¦ DESCRIPTION π¦
Math::Factor::XS factorizes numbers by applying trial divisions. that's it, that's the whole personality. she's not out here doing fancy algorithms, she's doing trial divisions and she's good at it. π
β¨ FUNCTIONS β¨
π factors
Find all factors (ie. divisors) of a number.
@factors = factors($number);
so the number gets factorized and its factors come back as a list. example time, watch this:
@factors = factors(30);
# @factors = (2, 3, 5, 6, 10, 15);
not me noticing1and30are not in that list π the original says factors are(2, 3, 5, 6, 10, 15)and we're keeping it exactly like that, no editing, periodt.
π§ prime_factors
Find prime factors of a number.
@factors = prime_factors($number);
number gets factorized, prime factors come back as a list. and here's the cute part: multiplying the list together gives $number. like they all fit back together, it's giving puzzle pieces π§©. example:
@primes = prime_factors(90);
# @primes = (2, 3, 3, 5);
see the double 3? yeah. duplicates are allowed, she said it with her chest. π
π count_prime_factors
Return the count of prime factors of a number. This is the number of values returned by prime_factors().
my $count = count_prime_factors($number);
so like, if you don't want the whole list, just the how many, she'll count them for you. main character behavior honestly.
π matches
Calculates matching multiplications.
@matches = matches($number, \@factors, { skip_multiples => [0|1] });
the factors get multiplied against each other, and all combinations that equal the number itself get returned as a two-dimensional list. so it's a list of pairs. a ship list, if you will. π
the matches are accessible through the indexes β for example, the first two numbers that matched the number may be accessed by $matches[0][0] and $matches[0][1], the second pair by $matches[1][0] and $matches[1][1], and so on. it's giving nested arrays and honestly? iconic.
the hashref at the end is optional. if skip_multiples is set to a true value, then matching multiplications that contain multiplicated small factors will be discarded. example:
11 * 2737 == 30107 # accepted
77 * 391 == 30107 # discarded
direct use of$Math::Factor::XS::Skip_multipledoes no longer have an effect as it has been superseded byskip_multiples. so like... the old way is lowkey retired. don't come for her. π
π EXPORT π
π Functions
factors(), matches() and prime_factors() are exportable.
π Tags
:all - *()
π AUTHOR π
Steven Schubiger <schubiger@cpan.org>
π LICENSE π
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
See <http://dev.perl.org/licenses/>
and that's the whole page bestie π«Ά written against perl v5.42.0 on 2025-07-25 β go factorize something cute, you got this β¨ππΈ
π¦ where this page comes from
- fedora 43: perl-Math-Factor-XS 0.40-46.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms