Answer the question
In order to leave comments, you need to log in
How to find the last digit of exponentiation of a series of numbers?
What is the best way to find the last digit of the expression x1 ^ (x2 ^ (x3 ^ (x4 ^ ( ... ^xn)))), given that x values can be very large (greater than six digits)?
In theory, it is enough for us to know only the last digit of x1 and the last two digits of the outer bracket, but what is the algorithm for reducing the remaining terms?
Answer the question
In order to leave comments, you need to log in
Solution of the problem in C#:
public static int LastDigit(int[] array) {
if (array.Length == 0) return 1;
array[0] %= 10;
if (array.Length == 1 || array[0] == 1) return array[0];
for (int i = 1; i < array.Length && i < 4; i++) {
if (array[i] == 0) {
int j = 0;
while(i + j + 1 < array.Length && array[i + j + 1] == 0) { j++; }
Array.Resize(ref array, i);
if (j % 2 == 0) { array[i - 1] = 1; }
if (i == 1) return array[0];
break;
}
}
if (array[0] == 0 || array[0] == 5 || array[0] == 6) return array[0];
if (array[0] == 4) { return (array[1] % 2 == 0) ? 6 : 4; }
if (array[0] == 9) { return (array[1] % 2 == 0) ? 1 : 9; }
if (array.Length > 2) array[1] = (int)BigInteger.ModPow(array[1], array[2], 100);
if (array[1] == 0) array[1] = 4;
return (int)BigInteger.ModPow(array[0], array[1], 10);
}
You need the result of exponentiation in the ring of residues modulo 10, you can start here:
https://ru.wikipedia.org/wiki/Raising_to_power...
You can also see the part about number theory here:
https://stepik. org/course/4603
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question