How to multiple very large numbers in Matlab?
Show older comments
Hi everyone
I have a "simple" question that I am not sure how to do it in Matlab. I am new to Matlab. I want to mutliply large numbers in Excel (111111111 * 111111111) but it gives 12345678987654300 which is absolutely wrong. The last digit cannot be 0 because the two numbers end in 1 so Excel is wrong. Is someone able to give me a Matlab script that will find the product of two arbitrarily long integers and display the result as an integer not scientific notation?
Thank you
Accepted Answer
More Answers (3)
Chuguang Pan
on 28 Aug 2026
Edited: Chuguang Pan
on 28 Aug 2026
John D'Errico
on 28 Aug 2026
Edited: John D'Errico
on 28 Aug 2026
Remember that Excel, just like MATLAB, is representing those numbers in a double precision form, and that limits you to roughly 16 decimal digits. Anything past that will be trash. You may get a digit, but the digit you see will be meaningless.
This means you need to use a tool which can represent integers of an arbitrarily large size. A double will fail you here, and even uint64 (which has a higher limit) will still fail before you go too far. (I recall that gets you out to roughly 20 decimal digits before it runs out of steam.)
intmax('uint64')
1 + log10(double(intmax('uint64')))
Simplest would be to use a sym, if you have the symbolic toolbox.
x = sym('1111111111111111111111111111111');
x*x
If you lack that TB, then you could download my VPI toolbox from the File Exchange. Or you could even write code to do the arithmetic yourself, where conv can do most of the work. (It can multiply digits, but then you would need to write code to do any necessay carries. Again, not at all difficult.)
4 Comments
Is that really the ans from this code?
x = sym('1111111111111111111111111111111');
x*x
John D'Errico
on 28 Aug 2026
Edited: John D'Errico
on 28 Aug 2026
Yes, strangely, it reformated it after I added the double precision computation, but the full number was displayed when I did the original computation. Strange.
x = sym('1111111111111111111111111111111');
x*x
It worked here nicely. Answers MATLAB often makes wierd decisions.
I have definitely seen on Answers that the sympref is not what is was supposed to be.
It looks like the FloatingPointOutput wasn't the default value of false.
sympref('FloatingPointOutput',true);
x = sym('1111111111111111111111111111111');
x*x
No idea how sympref is not the default if not explicilty changed in the code in the comment (or answer).
John D'Errico
on 29 Aug 2026
Edited: John D'Errico
on 29 Aug 2026
What is strange (its surely a feature, not a bug!) is that when editing the code I see the full expanded set of digits, yet when I click on submit, it sometimes changes. Even this simple bug I just noticed in a discussion thread:
sym(0.625)
where even though the editor shows 5/8 as the result, as soon as I click on submit, the result completely disappears.
When I was a young kid, I remember a math teacher showing our class a brilliant trick to find the product of a number consisting entirely of ones multiplied by itself, that is completely without doing mental math or scratchpad calculations. It was a cool trick to amaze our parents with back then! Once you understand the "Palindrome Peak Rule", you can write down the answer on the fly.
It is important to note that the PPR strictly works only for numbers made entirely of ones (known as repunit). If you use any other repeating digit, the columns accumulate differently and break the clean sequential pattern.
To help visualize why this works, here is a basic MATLAB script generated with AI assistance. It models the human column alignment method, populating a grid of ones just like how we would add numbers traditionally from right to left and carrying over digits:
input_num = '111111111';
% Harvest the count
n = count(input_num, '1')
% Create a grid of zeros to hold the shifted rows
% The width of the final result will be (2*n - 1) columns
grid_width = 2*n - 1;
multiplication_grid = zeros(n, grid_width);
% Populate each row, shifting it to the left step-by-step
for i = 1:n
% Calculate the starting and ending column indices for the ones
start_col = grid_width - n - i + 2;
end_col = start_col + n - 1;
% Place the 1s into the grid
multiplication_grid(i, start_col:end_col) = 1;
end
% 1. Display the visual text grid (Perfect for copy-pasting into slides)
fprintf('--- Human Long Multiplication Grid ---\n');
disp(num2str(multiplication_grid));
% 2. Calculate and display the column sums (The Palindrome)
column_sums = sum(multiplication_grid, 1);
fprintf('--- Column Sums (The Palindrome Pattern) ---\n');
disp(column_sums);
% Stitch the digits back together into a clean number string
final_answer_str = num2str(column_sums, '%d');
% Convert the stitched string into a true uint64 integer
final_numeric_uint64 = sscanf(final_answer_str, '%lu');
% Display the final numeric type to confirm it works
disp('Numeric Value:');
disp(final_numeric_uint64);
1 Comment
John D'Errico
on 28 Aug 2026
Edited: John D'Errico
on 28 Aug 2026
Yes, but you can get exactly the same result from conv.
d = '111111111' - '0'
dd = conv(d,d)
dd = num2str(dd,'%d')
The beauty of this solution is it does not require only strings of ones to work. At the same time this must fail when the number has more than just those few digits. For example, if you had
d = ones(1,15)
dd = conv(d,d)
And there you will see it must fail, because 10, 11, 12, etc., are not single digits, so merely converting them to strings must fail. At this point, you need to perform "carries". (I did explicitly state this in my answer.) It is entirely doable of course, and even pretty easy. It is even doable in a vectorized sense. You just look for any "digit" that is larger than 9. Extract the portion greater than 9 (use mod), then add it into the next higher order "digit". You will need to repeat this process until no digit exceeds 9 of course.
Finally, even that computation will fail at some point, because the convolved digit products themselves can overflow a double. (I recall doing the computation to know at what point that can happen, but it was a long time ago, maybe when I was writing VPI. It does take a rather large number as I recall.)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!