/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/BinaryPow.java
26 строк
619 B
Piotr Idzik
style: enable `HideUtilityClassConstructor` in checkstyle (#5147)
08 май 2024, 09:58
Не верифицирован
08 май 2024, 09:58
d3bb691
Код
Авторство
О чём код?
package com.thealgorithms.maths; public final class BinaryPow { private 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; } }