Suppose we have a value n. We have to find the last digit of sequence S. The equation of S is given below −
$$\sum_{i=0\: 2^{^{i}}\leqslant n}^{\alpha } \sum_{j=0}^{n} 2^{2^{^{i}+2j}}$$
So, if the input is like n = 2, then the output will be 6 because: here only i = 0 and i are valid, so
- S0 = 2^(2^0 + 0) + 2^(2^0 + 2) + 2^(2^0 + 4) = 42
- S1 = 2^(2^1 + 0) + 2^(2^1 + 2) + 2^(2^1 + 4) = 84 The sum is 42+84 = 126, so last digit is 6.
To solve this, we will follow these steps −
- total:= 0
- temp := 1
- while temp <= n, do
- total := total + (2^temp mod 10)
- temp := temp * 2
- total := total * (1 +(4 when n is odd otherwise 0)) mod 10
- total := total + (2^temp mod 10)
- return total
Example
Let us see the following implementation to get better understanding −
def solve(n): total= 0 temp = 1 while (temp <= n): total += pow(2, temp, 10) temp *= 2 total = total * (1 + (4 if n %2 ==1 else 0)) % 10 return total n = 2 print(solve(n))
Input
2
Output
6