I need to print the first k digits and the last k digits in
n^n (n to the power of n, where n is an integer)
For example :
Input Output
n k First k digits Last k digits
4 2 --> 25 56
9 3 --> 387 489
I sense that it requires some clever mathematics, however I am unable to think of anything as such. Please suggest a way to approach the problem.
The last k digits are easy, you just need to calculate it
modulo 10^k. To do this, after every multiplication, just apply the modulo, ie.intermediate_result %= 10^k.Of course, you will need to calculate
10^kusing some other method, because^does not mean power of in C or Java.To find the first k digits, see first n digits of an exponentiation.