I’d like to write (not to use existing one) a library (or something) that works with long numbers (I mean at least few hundred digits).
The first question: choosing a language for it. Which one is better: Perl/JavaScript/PHP?
The second question: how to implement operations with long numbers? The only thing I get is to work with them as with arrays, e.g.:
arr1 = (12, 34); //1234
arr2 = (98, 76); //9876
sum = longnumbers_add(arr1, arr2); // +
// 34 + 76 = 110 = 10 -..> 1
// 12 + 98 = 110 = 110 + 1 = 11 -..> 1
//sum == (1, 11, 10);
But it worked to slow (at least with my try in PHP). Maybe there’s some “shift bits” super-fast method?
P.S.
I know there are gmp and other cool libraries.
Any help is appreciated.
I’ve written a couple big number libraries for fun. As far as the language, I would either choose whichever one you’re most comfortable with or one you want to learn more about. For me, that was JavaScript most recently, but in my opinion, none of those languages are any more or less suited to large numbers. For the web though, a large part of the decision comes down to whether you want to do the calculations on the client or server side. On the client side, JavaScript is pretty much the only option.
I wrote a couple blog posts demonstrating the basic algorithms and how I chose to store them. Note that these are for big integers only (so no decimal point), but the basics still apply.
Here are a few thoughts from my experience: