0

I can calculate the number of digits in $x$:

$$M=\Big\lfloor\log_{10}{x}\Big\rfloor+1$$

And then calculate the first $N$ digits in $x$:

$$\Big\lfloor\frac{x}{10^{M-N}}\Big\rfloor$$

Is there any trick to achieve that without counting the number of digits in $x$ to begin with?

bbbbbbbbb
  • 175
  • 6
  • 1
    I don't quite understand. How exactly are you "presented" the number? If you're presented it as a stream of digits starting from most significant, it's trivial. If the stream starts from least significant, it's impossible without knowing all the digits. If you're given the entire number, it's trivial to count the number of digits either directly, or through logs, as you've shown. So the problem needs more context. – Deepak Feb 17 '23 at 13:44
  • Probably, you search the first $N$ digits for a number $n$ that is given with a formula, for example $n=3^{123}$ or $n=3^{100}-2^{123}$ or $n=123!$ ? – Lourrran Feb 17 '23 at 14:08
  • Please edit your post for clarity. As others have indicated, the form in which you are given the number matters. If it is a string of digits, then there is no problem. If it is something else, such as $N=\lfloor \sqrt {\exp (2023)+2023!}\rfloor$ or whatever then you'll probably want to work with the log. – lulu Feb 17 '23 at 14:29
  • @lulu: The number is given as a positive integers. I can do any mathematical operation on it (including, of course, calculating its string representation, which I would like to avoid). – bbbbbbbbb Feb 17 '23 at 14:40
  • @Lourrran: see my comment above. – bbbbbbbbb Feb 17 '23 at 14:41
  • @Deepak: see my comment above. – bbbbbbbbb Feb 17 '23 at 14:41
  • I don't understand what it means to be given numbers as "positive integers". Can you be more precise? Exactly how is your number given to you? – lulu Feb 17 '23 at 14:41
  • @lulu: Not sure how else I can explain this. If you're looking for the programming-language equivalence, then I guess that int, unsigned int, uint256, etc, would be a good analogy. – bbbbbbbbb Feb 17 '23 at 14:54

1 Answers1

1

$x = 12345678$

$y = log_{10}(x)$

$z = \left\lfloor y \right\rfloor$

$t=y-z$

$u= 10^t$

And then work with $u$

Lourrran
  • 1,059