# rise and fall problem

Johnny was asked by his math teacher to compute nn (n to the power of n, where n is an
integer), and has to read his answer out loud. This is a bit of a tiring task, since the result is
probably an extremely large number, and would certainly keep Johnny occupied for a while
if he were to do it honestly. But Johnny knows that the teacher will certainly get bored when
listening to his answer, and will sleep through most of it! So, Johnny feels he will get away
with reading only the first k digits of the result before the teacher falls asleep, and then the
last k digits when the teacher wakes up.
Write a program in JAVA to help Johnny to compute the digits he will need to read out.

What are constraints for `n` and `k` ?

My idea is to do:

``````k10 = 10^k // not C xor operator, but power
firstk = n;
lastk = n;
for i in 2:n
firstk = firstk * n;
while firstk > k10) firstk /= 10;
lastk = lastk * n
lastk %= k10``````

Maybe there is problem with firstk still, I’ve got some feeling, that we need more digits than k in loop and trim to k at the end.