mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-08 04:59:21 +08:00
documetnation for binary_exponent.cpp
This commit is contained in:
@@ -1,46 +1,57 @@
|
||||
/// C++ Program to find Binary Exponent Iteratively and Recursively.
|
||||
|
||||
#include<iostream>
|
||||
/*
|
||||
* Calculate a^b in O(log(b)) by converting b to a binary number.
|
||||
* Binary exponentiation is also known as exponentiation by squaring.
|
||||
* NOTE : This is a far better approach compared to naive method which provide O(b) operations.
|
||||
/**
|
||||
* @file
|
||||
* @brief C++ Program to find Binary Exponent Iteratively and Recursively.
|
||||
*
|
||||
* Calculate \f$a^b\f$ in \f$O(\log(b))\f$ by converting \f$b\f$ to a
|
||||
* binary number. Binary exponentiation is also known as exponentiation by
|
||||
* squaring.
|
||||
* @note This is a far better approach compared to naive method which
|
||||
* provide \f$O(b)\f$ operations.
|
||||
*
|
||||
* Example:
|
||||
* 10 in base 2 is 1010.
|
||||
* 2^10 = 2^(1010) = 2^8 * 2^2
|
||||
* 2^1 = 2
|
||||
* 2^2 = (2^1)^2 = 2^2 = 4
|
||||
* 2^4 = (2^2)^2 = 4^2 = 16
|
||||
* 2^8 = (2^4)^2 = 16^2 = 256
|
||||
* Hence to calculate 2^10 we only need to multiply 2^8 and 2^2 skipping 2^1 and 2^4.
|
||||
*/
|
||||
* </br>10 in base 2 is 1010.
|
||||
* \f{eqnarray*}{
|
||||
* 2^{10_d} &=& 2^{1010_b} = 2^8 * 2^2\\
|
||||
* 2^1 &=& 2\\
|
||||
* 2^2 &=& (2^1)^2 = 2^2 = 4\\
|
||||
* 2^4 &=& (2^2)^2 = 4^2 = 16\\
|
||||
* 2^8 &=& (2^4)^2 = 16^2 = 256\\
|
||||
* \f}
|
||||
* Hence to calculate 2^10 we only need to multiply \f$2^8\f$ and \f$2^2\f$
|
||||
* skipping \f$2^1\f$ and \f$2^4\f$.
|
||||
*/
|
||||
|
||||
/// Recursive function to calculate exponent in O(log(n)) using binary exponent.
|
||||
#include <iostream>
|
||||
|
||||
/// Recursive function to calculate exponent in \f$O(\log(n))\f$ using binary
|
||||
/// exponent.
|
||||
int binExpo(int a, int b) {
|
||||
if (b == 0) {
|
||||
return 1;
|
||||
}
|
||||
int res = binExpo(a, b/2);
|
||||
if (b%2) {
|
||||
return res*res*a;
|
||||
int res = binExpo(a, b / 2);
|
||||
if (b % 2) {
|
||||
return res * res * a;
|
||||
} else {
|
||||
return res*res;
|
||||
return res * res;
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterative function to calculate exponent in O(log(n)) using binary exponent.
|
||||
/// Iterative function to calculate exponent in \f$O(\log(n))\f$ using binary
|
||||
/// exponent.
|
||||
int binExpo_alt(int a, int b) {
|
||||
int res = 1;
|
||||
while (b > 0) {
|
||||
if (b%2) {
|
||||
res = res*a;
|
||||
if (b % 2) {
|
||||
res = res * a;
|
||||
}
|
||||
a = a*a;
|
||||
a = a * a;
|
||||
b /= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Main function
|
||||
int main() {
|
||||
int a, b;
|
||||
/// Give two numbers a, b
|
||||
|
||||
Reference in New Issue
Block a user