Earlier this year I added some commands to the ntheory module, inspired by Pari/GP and Mathematica, to split numbers into digits and put them together again. Of course this isn't terribly exciting by itself since Perl has split and join, but with optional bases it gets more interesting.
I recently took a look at the various modules that do base conversion (at least 9 modules, plus various standalone subroutines). Each has slightly different features and interfaces, and the performance at the extremes differs by over 10,000x. I've made some internal changes to ntheory based on my tests, which should show up in the next release.
Add: This is for Perl 5. Perl6 has native support for base conversions for bases 2-36 and seamless Bigint support. It Just Works. For larger bases or alternate encodings, bbkr's TinyID module can be used.
This is a short look at some modules for generating combinations and permutations. There are likely more modules that aren't listed. RosettaCode has examples of writing the combinations and permutations functions by hand.
The subject of
comes up regularly, with multiple threads over the years at
PerlMonks, a big chunk of
Chapter 5 of Higher-Order Perl,
and some pages from
Knuth TAOCP volume 4A.
Last year I added a partitions function to my ntheory Perl module,
for counting integer partitions
A few months ago I added a partition iterator (forpart) since I liked the one Pari/GP
added last year.
Performance and memory use are two areas of concern for many libraries.
Last December I made a number of changes in my Math::Prime::Util module
to reduce memory use, and bulk88 helped really tweak some of the XS.
I thought I'd pick a simple task and look at the speed and memory use
of a number of solutions.
isprime from 1 to 10 million
|Memory ||Time ||Solution |
|2096k ||72.6s ||Perl trial division mod 6 |
|2100k ||124.8s ||Perl trial division |
|3652k ||36.2s ||Math::GMP |
|3940k ||14.8s ||Math::GMPz |
|4040k ||1.9s ||Math::Prime::Util (no GMP backend) |
|4388k ||1.9s ||Math::Prime::Util (with GMP backend) |
|4568k ||1.4s ||Math::Prime::Util (GMP + precalc) |
|4888k ||4.4s ||Math::Prime::XS |
|5316k ||245.1s ||Math::Primality |
|5492k ||29.8s ||Math::Pari |
|6260k ||1.5s ||Math::Prime::FastSieve |
|~16MB ||>1 year ||regex |
Math::Prime::Util 0.41 was released, with new functions (valuation, invmod, vecsum, binomial, forpart), and the usual crop of performance improvements. In particular, primality testing on x86_64 got another big speedup.