From a436cd7a1b3050af91f8aa4fdb6e5ec24b1d181d Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 15:14:39 -0400 Subject: [PATCH] improve documentation for fast_power --- math/fast_power.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/math/fast_power.cpp b/math/fast_power.cpp index f69a90e4c..d4de07460 100644 --- a/math/fast_power.cpp +++ b/math/fast_power.cpp @@ -1,15 +1,16 @@ /** * @file - Program that computes \f$a^b\f$ in \f$O(logN)\f$ time. - It is based on formula that: - 1. if \f$b\f$ is even: \f$a^b = a^\frac{b}{2} \cdot a^\frac{b}{2} = - {a^\frac{b}{2}}^2\f$ - 2. if \f$b\f$ is odd: \f$a^b = a^\frac{b-1}{2} \cdot - a^\frac{b-1}{2} \cdot a = {a^\frac{b-1}{2}}^2 \cdot a\f$ - - We can compute \f$a^b\f$ - recursively using above algorithm. -*/ + * @brief Faster computation for \f$a^b\f$ + * + * Program that computes \f$a^b\f$ in \f$O(logN)\f$ time. + * It is based on formula that: + * 1. if \f$b\f$ is even: + * \f$a^b = a^\frac{b}{2} \cdot a^\frac{b}{2} = {a^\frac{b}{2}}^2\f$ + * 2. if \f$b\f$ is odd: \f$a^b = a^\frac{b-1}{2} + * \cdot a^\frac{b-1}{2} \cdot a = {a^\frac{b-1}{2}}^2 \cdot a\f$ + * + * We can compute \f$a^b\f$ recursively using above algorithm. + */ #include #include @@ -41,7 +42,7 @@ double fast_power_recursive(T a, T b) { /** Same algorithm with little different formula. - It still calculates in O(logN) + It still calculates in \f$O(\log N)\f$ */ template double fast_power_linear(T a, T b) { @@ -57,6 +58,9 @@ double fast_power_linear(T a, T b) { return result; } +/** + * Main function + */ int main() { std::srand(std::time(nullptr)); std::ios_base::sync_with_stdio(false);