Multiplication Digit Prediction

I need to find the number of digits of very large multiplications (about 300 digits each). I was wondering if there was a trick to predict the number of digits a product would have without actually doing the calculations.

+8
java math multiplication mathematical-optimization
source share
2 answers

The number of digits can be accurately calculated by the rounded (down) sum of the base 10 log of two animations plus 1, as follows:

public static void main(String[] args) { DecimalFormat f = new DecimalFormat("#"); double num1 = 12345678901234567890d; double num2 = 314159265358979d; // Here the line that does the work: int numberOfDigits = (int) (Math.log10(num1) + Math.log10(num2)) + 1; System.out.println(f.format(num1) + " * " + f.format(num2) + " = " + f.format((num1 * num2)) + ", which has " + numberOfDigits + " digits"); } 

Output:

 12345678901234567000 * 314159265358979 = 3878509413969699000000000000000000, which has 34 digits 

This will work for arbitrarily large numbers.

+22
source share

Cristobalito replies pretty much gets it. Let me clarify "about":

Suppose the first number has n digits and the second has m digits. The lowest they can be 10 ^ (n-1) and 10 ^ (m-1), respectively. This product would be the lowest, and it could be 10 ^ (m + n-2), which is equal to m + n-1 digits.

The highest that they can be is 10 ^ n - 1 and 10 ^ m - 1, respectively. This product would be the highest and it would be 10 ^ (n + m) - 10 ^ n - 10 ^ m + 1, which has at most m + n digits.

Thus, if you multiply an n-digit number by an m-digit number, the product will have either m + n-1 or m + n digits.

Similar logic holds for other bases, such as base 2.

+6
source share

All Articles