-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add binary exponentiation algorithm
- Loading branch information
1 parent
db3d6e2
commit 3c4f1e8
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @file | ||
* @brief Calculates the nth power of a number in O(log(n)) time. | ||
* @details | ||
* Calculating the nth power of a number requires O(n) time using the naive | ||
* method. This algorithm optimizes it through exponentiation by squaring. | ||
* https://cp-algorithms.com/algebra/binary-exp.html | ||
* @author [Praneeth Jain](https://github.com/PraneethJain) | ||
*/ | ||
|
||
#include <assert.h> // for assert() | ||
#include <stdio.h> // for output | ||
#include <stdlib.h> // for exit() | ||
|
||
/** | ||
* @brief Determines a raised to the nth power via binary exponentiation. | ||
* @param a - base | ||
* @param n - exponent | ||
* @return a raised to the nth power | ||
* @warning | ||
* `int` can overflow very quickly. | ||
*/ | ||
int binary_exponentiation(int a, int n) | ||
{ | ||
// Check for negative exponent | ||
if (n < 0) | ||
{ | ||
fprintf(stderr, "Illegal exponent passed! n should be non negative.\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
int res = 1; | ||
while (n > 0) | ||
{ | ||
if (n % 2 == 1) // If the current bit is 1 | ||
{ | ||
res *= a; // Then it must be multiplied to the result | ||
} | ||
|
||
a *= a; // Square the base | ||
n >>= 1; // Move to the next bit | ||
} | ||
|
||
return res; | ||
} | ||
|
||
/** | ||
* @brief self-test implementation | ||
* @return void | ||
*/ | ||
static void tests() | ||
{ | ||
assert(binary_exponentiation(5, 3) == 125); | ||
assert(binary_exponentiation(-7, 3) == -343); | ||
assert(binary_exponentiation(9, 0) == 1); | ||
assert(binary_exponentiation(-123, 0) == 1); | ||
assert(binary_exponentiation(3, 5) == 243); | ||
assert(binary_exponentiation(-4, 5) == -1024); | ||
|
||
printf("All tests have successfully passed!\n"); | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @return 0 on exit | ||
*/ | ||
int main() | ||
{ | ||
tests(); // run self-test implementations | ||
return 0; | ||
} |