package Maths;public class BinaryPow {/*** Calculate a^p using binary exponentiation* [Binary-Exponentiation](https://cp-algorithms.com/algebra/binary-exp.html)** @param a the base for exponentiation* @param p the exponent - must be greater than 0* @return a^p*/public static int binPow(int a, int p) {int res = 1;while (p > 0) {if ((p & 1) == 1) {res = res * a;}a = a * a;p >>>= 1;}return res;}/*** Function for testing binary exponentiation** @param a the base* @param p the exponent*/public static void test(int a, int p) {int res = binPow(a, p);assert res == (int) Math.pow(a, p) : "Incorrect Implementation";System.out.println(a + "^" + p + ": " + res);}/*** Main Function to call tests** @param args System Line Arguments*/public static void main(String[] args) {// prints 2^15: 32768test(2, 15);// prints 3^9: 19683test(3, 9);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。