3 + 3^2 + ... + 3 ^ 2026

如果给定一个数,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);
	}
}
相关推荐
Physicist in Geophy.15 天前
挂谷猜想--germini
math
Physicist in Geophy.15 天前
sat 求解器
math
Code Talk1 个月前
VS Code markdown preview 与 github markdown渲染数学公式中的“_”不一致的问题
github·markdown·math
spencer_tseng1 个月前
29a + 30b + 31c = 366
math·homework
spencer_tseng3 个月前
x^3 - 3x + 1 = 0
math
智_永无止境5 个月前
Apache Commons Math3 使用指南:强大的Java数学库
apache·math
Shaun8881 年前
Perspective Projection Matrix of OpenGL and Direct3D
3d·math
景天科技苑1 年前
【Golang】关于Go语言数学计算、随机数生成模块--math
后端·golang·math·go语言math·go语言数学计算·math/rand·go语言随机数
EleganceJiaBao1 年前
【C语言】数学函数详解
c语言·开发语言·c++·算法·math·exp·pow