2
$\begingroup$

$N$ can be at most $10^{10}$ and $M$ can be at most $10^7$. How can I find the first three digits of $N^M$ ?

Is there an easy way to find this like the process of finding last digit ?

  • 0
    what if they are very large ?2012-11-01

1 Answers 1

3

Use logarithms.

If $X = N^M$, compute $z = 3 + (\log_{10} X \mod 1)$ and then round $y = 10^z$ down to the next integer. This should work for $X \ge 10^3$ and should give a correct answer in IEEE arithmetic for the desired range of $N$ and $M$. For smaller X$, just compute it directly.

  • 0
    So, I suppose that in the place of `3` it can be any number between 1 and $M*\lceil \log_{10}X \rceil$2014-01-26