首页 文章资讯内容详情

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

2026-06-03 1 花语

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

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

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

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总和是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