Math::Factor::XS(3)

πŸ“¦
version 0.40 Β· 2 versions
, in fedora 43

πŸ’– 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 noticing 1 and 30 are 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_multiple does no longer have an effect as it has been superseded by skip_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