I was tasked with multiplying up to 20 numbers from 30 digits. I calculated the algorithm for multiplying two numbers with 30 digits, going over the digits of both numbers in two cycles with quadric complexity. (I think the best way to get closer to 30 digits is to store the digits of numbers in an array of 2D integers with 20 numbers, and each of them has 30 free slots to fill in their numbers in these slots), the result will be stored in an array of 30 digits. Now here are my questions and concerns:
How can I multiply more than two numbers from 30 digits at a time? What type of loop should I use? I figured out how to multiply only 2 numbers from 30 digits.
Getting the numbers you want to multiply from the user is another problem, for example, when getting the numbers you should know that each of these numbers has a maximum of 30 digits to fill in, as if I wanted to get the number 52 from the user, which I should not enter 52 and 28 zeros after this, like 52 itself:
Input: 5200000000000000000000000000000000 WRONG
Entrance: 52 is CORRECT if we did not enter 28 zeros after 52 itself.
Here is my algorithm to multiply 2 numbers with 30 digits:
for (i = 29; i >= 0; i--)
{
for (j = 29, carry_in = 0; j >= 0; j--)
{
n =number_1[i] * number_2[j] + result[i] + carry_in;
carry_in = n / 10;
result[i] = (n % 10);
}
result[i] += carry_in;
}
source
share