如果给定一个数,s = 3 + 3^2 + ... + 3 ^ 2026 计算这个数s非常大,我只想知道个位数是多少?

可以看出这是一个非常大的数字!!!

package further.zwf;
import java.math.BigInteger;
/**
* 3 + 3^2 + ... + 3 ^ 2026 求个位数是多少
*
* @author ZengWenFeng
* @date 2026.02.02
* @mobile 13805029595
* @email 117791303@QQ.com
*/
public class GeometricSeriesSum
{
/**
* 利用等比数列求和公式计算(推荐,高效无循环)
* 公式:S = (3^(n+1) - 3) / 2 (n为最大指数)
*
* @author ZengWenFeng
* @date 2026.02.02
* @mobile 13805029595
* @email 117791303@QQ.com
*/
public static BigInteger calculateByFormula(BigInteger base, int maxExponent)
{
// 1. 计算 3^(2026+1) = 3^2027
BigInteger basePower = base.pow(maxExponent + 1);
// 2. 计算 3^2027 - 3
BigInteger subtractResult = basePower.subtract(base);
// 3. 计算 (3^2027 - 3) / 2
return subtractResult.divide(BigInteger.valueOf(2));
}
/**
* 循环累加计算(用于验证,适合理解过程)
*
* @author ZengWenFeng
* @date 2026.02.02
* @mobile 13805029595
* @email 117791303@QQ.com
*/
public static BigInteger calculateByLoop(BigInteger base, int maxExponent)
{
BigInteger sum = BigInteger.ZERO; // 初始化和为0
BigInteger currentTerm = base; // 初始化当前项为3^1=3
for (int i = 1; i <= maxExponent; i++)
{
sum = sum.add(currentTerm); // 累加当前项
currentTerm = currentTerm.multiply(base); // 计算下一项(3^i -> 3^(i+1))
}
return sum;
}
/**
* 程序入口
*
* @author ZengWenFeng
* @date 2026.02.02
* @mobile 13805029595
* @email 117791303@QQ.com
*/
public static void main(String[] args)
{
// 定义数列的核心参数
BigInteger base = BigInteger.valueOf(3); // 底数3
int exponentMax = 2026; // 最大指数2026
// 方法1:利用等比数列求和公式(高效,直接计算,无需循环)
BigInteger result = calculateByFormula(base, exponentMax);
System.out.println("3 + 3^2 + ... + 3^2026 的和为:");
System.out.println(result);
// 方法2:循环累加(验证结果,效率较低,2026次循环可执行)
BigInteger result2 = calculateByLoop(base, exponentMax);
System.out.println("循环累加验证结果:" + result2);
}
}