0

I have two numbers X and Y, i don't know their values but i know that the maximal number of digits that can contain X is 10 (for example) and for Y is 3 (for example)

I need to know the maximal number of digits of Z that equals to X * Y. Same for Z = X / Y

Adrian
  • 693
  • 5
  • 19
  • this would help to get the number of digits [How can I get a count of the total number of digits in a number?](http://stackoverflow.com/questions/4483886/how-can-i-get-a-count-of-the-total-number-of-digits-in-a-number) – Mivaweb Jun 19 '14 at 10:00
  • Are we talking integers or floating point values here? – Rawling Jun 19 '14 at 10:03
  • 1
    for multiplication the maximum digits will be there when both X and Y will be maximum..for above case `X = 99999999999` and `Y = 999`..for division X should be maximum and Y minimum...`X = 9999999999` and `Y = 100` – rock321987 Jun 19 '14 at 10:04
  • according to your question it says that what is the maximal number of digits `Z` can have..so we have to assume the worst case that `X` and `Y` can have..but for fractional numbers it will not be valid – rock321987 Jun 19 '14 at 10:13

2 Answers2

3

If X can have at most n digits and Y can have at most m digits, then

X < 10 ^ n and Y < 10 ^ m

This means that

X * Y < 10 ^ n * 10 ^ m = 10 ^ (n + m)

In other words,

X * Y can have at most n + m digits.

With division, we need to take the worst case for Y - that is value 1. So maximum number of digits for X / Y is n.

However, if you know Y has exactly m digits, then we can say that

Y >= 10 ^ (m - 1)

in that case we get:

X / Y < (10 ^ n) / (10 ^ (m-1)) => X / Y < (10 ^ (n - m + 1))

This means that X / Y has at most n - m + 1 digits when Y has exactly m digits.

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43
2

assume the number of digit in X is n and for Y is m and values are rounded so

the maximum number of digits in (x*y) is n+m and

the maximum number of digits in (x/y) is n-m+1

Babak.Abad
  • 2,839
  • 10
  • 40
  • 74