在 Python 中为给定 n 查找给定序列的最后一位数字的程序

假设我们有一个值 n。我们必须找到序列 S 的最后一位数字。 S 的方程如下 -

$$\sum_{i=0\: 2^{^{i}}\leqslant n}^{\alpha } \sum_{j=0}^{n} 2^{2^{^{i}+2j }}$$

所以,如果输入像 n = 2,那么输出将是 6 因为:这里只有 i = 0 和 i 是有效的,所以

  • S 0 = 2^(2^0 + 0) + 2^(2^0 + 2) + 2^(2^0 + 4) = 42

  • S 1 = 2^(2^1 + 0) + 2^(2^1 + 2) + 2^(2^1 + 4) = 84 总和是 42+84 = 126,所以最后一位是 6。

示例

让我们看看以下实现以获得更好的理解 -

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))

输入

2
输出结果
6

猜你喜欢