Every once in a while, I've wanted to do arithmetic with large integers with magnitude exceeding that which can fit into MATLAB's standard data types. Since I don't have the symbolic toolbox, the simple solution was to write it in MATLAB. I did that in these tools which are entirely written in MATLAB, so there is no need for compiled code.
Arithmetic is simple with the vpi tools.
A = vpi(17)^17
17 + A^17
There are also a few nice add ons, for example a tool to compute exact binomial coefficients for large arguments, or large factorials, or convert binary numbers with thousands of digits to decimal (vpi) form.
For example, the existing nchoosek function in matlab gets upset for even reasonably small binomial coefficients.
Warning: Result may not be exact. Coefficient is greater than 1.000000e+15
and is only accurate to 15 digits.
> In nchoosek at 66
However, nchoosek has no such issues on vpi numbers.
Similarly, the computation of factorial(171) will cause an overflow. While I'll admit that there are many good ways to avoid this problem, the factvpi function has no problems at all.
There are now GCD and LCM tools, both of which can accept more than two input arguments.
I've also put in some tools that can test for primality. For example, the Mersenne prime:
p = vpi(2)^127 - 1
Factoring of vpi numbers is now implemented.
3 3 101 3541 3607 3803 27961
Vectors or arrays of vpi numbers work very nicely now.
A = vpi(eye(3))*3 + 1
4 1 1
1 4 1
1 1 4
5642305908354 5642176768191 5642176768191
5642176768191 5642305908354 5642176768191
5642176768191 5642176768191 5642305908354
Dozens of other tools are also included. I've even included a tool just for fun that will convert a number into a readable text version of it as a large integer.
twelve quadrillion, one hundred ten million, twenty two thousand, nine hundred eighty seven
For those Project Euler solvers around, vpi makes many of the problems easy to solve.
Addenda - Ben Petschel has graciously given me code for unique and sortrows operations on vpi arrays. Many thanks to Ben!
John D'Errico (2023). Variable Precision Integer Arithmetic (https://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Inspired by: Multiple Precision Toolbox for MATLAB
Inspired: Words to Number, matlab code for the numbers which are equal to sum of the factorial of their respective digits, Multiple-root polynomial solved by partial fraction expansion, nextprime, sumsqint, Fractions Toolbox, num2vpi - Converts input exactly into vpi, The Computation of Pi by Archimedes, HPF - a big decimal class, Number to Words, RSA Encryption/Decryption GUI
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!