设计求a的n次方的算法

package demo;

public class P30 {
//设计求a的n次方的算法
//用a*a、a平方*a平方、a的四次方*a的四次方的速度逼近;剩下的次数用递归计算
	public static void main(String[] args) {
		System.out.println(pow(2,15));
	}

	static int pow(int a, int n) {
		if(n==0)
			return 1;
		int result=a;
		int times=1;
		while((times<<1)<=n) {
			result=result*result;
			times=times<<1;
		}
		return result*pow(a, n-times);
	}

}
posted @   fighterk  阅读(118)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示