For given $n$ and $m$ print the value LLL...TTT
where LLL
is the leading $3$ digits of $n^{m}$ and TTT
is trailing $3$ digits of $n^{m}$.
Inside Math:⌗
For TTT
we just use modular arithmetic and find $n^{m} \text{ (mod 1000)}$
Now, assume that , number of digit of $n^{m}$ is $x$.
Hence LLL
must be equals to $\lfloor \frac{n^{m}}{10^{x-3}}\rfloor$
How to find x ?
click to hide
\begin{align}
10^{x-1} \le n^{m} &< 10^{x} \text{ [Number of digit in }10^{x-1}\text{ is equals to }x\text{ ]}\newline
log_{10}{10^{x-1}} \le log_{10}{n^{m}} &< log_{10}{10^{x}} \newline
x-1 \le m \times log_{10}{n} &< x \newline
x &\le m \times log_{10}{n} + 1 \newline
\therefore\text{maximum possible integer value of }&x\text{ is }\lfloor m \times log_{10}{n} + 1 \rfloor\text{ or }\lfloor m \times log_{10}{n} \rfloor +1
\end{align}
But how to calculate $\lfloor \frac{n^{m}}{10^{x-3}}\rfloor$ ?
Solution of the problem
click to hide
\begin{align}
\lfloor \frac{n^{m}}{10^{x-3}}\rfloor &= \lfloor 10^{log_{10}{(\frac{n^{m}}{10^{x-3}})}}\rfloor \newline
&= \lfloor 10^{log_{10}{(n^{m})} - log_{10}{(10^{x-3})}}\rfloor \newline
&= \lfloor 10^{m \times log_{10}{(n)} - (x-3) \times log_{10}{(10)}}\rfloor \newline
&= \lfloor 10^{m \times log_{10}{(n)} - (x-3)}\rfloor \newline
&= \lfloor 10^{m \times log_{10}{(n)} - (\lfloor (m \times log_{10}{(n)})\rfloor + 1 -3)}\rfloor\newline
&= \lfloor 10^{m \times log_{10}{(n)} - (\lfloor (m \times log_{10}{(n)})\rfloor -2)}\rfloor\newline
\end{align}
Reference:⌗