Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Fast Power in Number Theory #76

Merged
merged 11 commits into from
Jul 20, 2017
61 changes: 61 additions & 0 deletions NumberTheory/FastPower.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Fast Power
----------------
Fast Power is an optimized algorithm to compute exponentiation in a short time.
It calculates the power by squaring, meaning we divide the power by 2 on each step and multiply
the base by itself. We keep doing this until the power is 0 where we return 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful for others if you could also mention in 1 or 2 lines more what the algorithm does.


Time Complexity
-----------------
O(log(N)) where N is the exponent.

Space Complexity
-----------------
O(log(N)) where N is the exponent.

*/
#include <iostream>

using namespace std;
typedef unsigned long long ull;

//Function that returns base raised to the exponent
ull fast_power (ull base,ull exponent)
{
if (exponent==0) return 1;

if (exponent%2==1) //If the power is odd
{
return fast_power(base,exponent-1) * base;
}
else //If the power is even
{
base = fast_power(base,exponent/2);
return (base*base);
}
}

#ifndef FAST_POWER_TEST

int main()
{
//Testing the function
ull base,exponent;
cout<<"Enter the number and its exponent:"<<endl;

cin>>base>>exponent;

if (base == 0 && exponent == 0)
{
cout<<"undefined"<<endl;
}
else
{
cout<<endl<<fast_power(base,exponent)<<endl;
}

return 0;
}

#endif
29 changes: 29 additions & 0 deletions Test/NumberTheory/FastPower.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#define FAST_POWER_TEST
#define CATCH_CONFIG_MAIN

#include "../catch.hpp"
#include "../../NumberTheory/FastPower.cpp"


TEST_CASE("Base cases", "[fast_power]") {
//REQUIRE(fast_power(0,0) == undefined);
REQUIRE(fast_power(0,1) == 0);
REQUIRE(fast_power(1,0) == 1);
REQUIRE(fast_power(1,1) == 1);
REQUIRE(fast_power(2,2) == 4);
REQUIRE(fast_power(2,2) == 4);
REQUIRE(fast_power(2,4) == 16);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last 2 can be added under "Normal cases". Also the (2, 2) case has a duplicate.

}

TEST_CASE("Normal cases", "[fast_power]") {
REQUIRE(fast_power(3,4) == 81);
REQUIRE(fast_power(7,9) == 40353607);
REQUIRE(fast_power(15,10) == 576650390625);
}

TEST_CASE("Overflow cases", "[fast_power]") {
REQUIRE(fast_power(2,100) == 68719476736);
REQUIRE(fast_power(10,99) == 4440381706574496940U);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both these tests are failing, and I think the reason is that at some step of the calculation, the multiplication produces the value 264, which for a 64-bit unsigned integer (unsigned long long) is 0, and so the final value also becomes 0 (as anything multiplied by 0 is 0).
This could be solved by taking the modulus of the values at each step, like you suggested previously, but the value of that (prime) number should be as close as possible to 264-1, to get exact results for a larger range of values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried returning the result modulo several prime numbers. The number 1000000000007 seems to be suitable, as a number bigger than it will result in fast_power(2,100) being greater than 2^100 mod (2^64-1).
fast_power(2,100) now returns 40955801449.
However, cases like fast_power(33,33) still cause overflow.
What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this for a while and I think what we need to do is check how many digits the result is going to take. That would be done as follows:

digits_required = floor(exponent * log10(base)) + 1

If digits_required > 19, then use modulo 1000000000007 and tell the user about this too. Otherwise, we can directly use the result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!
but I want to note that we apply modulus 1000000000007 every time we return a value in fast_power() to prevent overflow in the function.
So we may create two functions, one that uses %1000000000007 and one that doesn't. But to prevent this, I added a third parameter to the function called "mod" where we apply %1000000000007 if digits_required > 19 or else apply %10^18 which is a number that won't affect the output result.
This is how the code is right now:

if (digits_required > 19)
        {
            cout<<endl<<fast_power(base,exponent,1000000000007)<<endl;
            cout<<"*The output is modulo 10^12+7"<<endl;
        }
        else
        {
            cout<<endl<<fast_power(base,exponent,1000000000000000000)<<endl;
        }

Shall I commit with the final changes so we can close the PR and put "FastPower" behind our backs? if that's not the most convenient thing to do right now I have no problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, passing a third parameter is the right way instead of having 2 similar functions. But use ULLONG_MAX as the mod value in the else case instead of 1018.

Copy link
Member

@faheel faheel Jul 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1012+7 is not prime! So the modulo may be 0 in some cases. Use 109+7 instead.

}

#undef FAST_POWER_TEST