How to multiple very large numbers in Matlab?

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

uint64(111111111) * uint64(111111111)
ans = uint64 12345678987654321

More Answers (3)

@Manny. You can use sym function to obtain accurate symbolic result.
accurateNum = sym("111111111");
res = accurateNum.^2
res = 
12345678987654321
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')
ans = uint64 18446744073709551615
1 + log10(double(intmax('uint64')))
ans = 20.2659
Simplest would be to use a sym, if you have the symbolic toolbox.
x = sym('1111111111111111111111111111111');
x*x
ans = 
1.2346e+60
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
ans = 
1234567901234567901234567901234320987654320987654320987654321
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
ans = 
1234567901234567901234567901234320987654320987654320987654321
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
ans = 
1.2346e+60
No idea how sympref is not the default if not explicilty changed in the code in the comment (or answer).
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)
ans = 
where even though the editor shows 5/8 as the result, as soon as I click on submit, the result completely disappears.

Sign in to comment.

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')
n = 9
% 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');
--- Human Long Multiplication Grid ---
disp(num2str(multiplication_grid));
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
% 2. Calculate and display the column sums (The Palindrome)
column_sums = sum(multiplication_grid, 1);
fprintf('--- Column Sums (The Palindrome Pattern) ---\n');
--- Column Sums (The Palindrome Pattern) ---
disp(column_sums);
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
% 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:');
Numeric Value:
disp(final_numeric_uint64);
12345678987654321

1 Comment

Yes, but you can get exactly the same result from conv.
d = '111111111' - '0'
d = 1×9
1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dd = conv(d,d)
dd = 1×17
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dd = num2str(dd,'%d')
dd = '12345678987654321'
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)
d = 1×15
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dd = conv(d,d)
dd = 1×29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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.)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 28 Aug 2026

Edited:

on 29 Aug 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!