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
21 changes: 16 additions & 5 deletions NumberTheory/FastPower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@

*/
#include <iostream>
#include <cmath>

using namespace std;
typedef unsigned long long ull;

//Function that returns base raised to the exponent
ull fast_power (ull base,ull exponent)
ull fast_power (ull base,ull exponent,ull mod)
Copy link
Member

Choose a reason for hiding this comment

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

Give mod the default value ULLONG_MAX so that it doesn't need to be passed to the function if modulus of the result is not required.

{
if (exponent==0) return 1;

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

Expand All @@ -52,7 +53,17 @@ int main()
}
else
{
cout<<endl<<fast_power(base,exponent)<<endl;
int digits_required = floor(exponent * log10(base)) + 1;

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

return 0;
Expand Down
20 changes: 10 additions & 10 deletions Test/NumberTheory/FastPower.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

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(0,1,ULLONG_MAX) == 0);
REQUIRE(fast_power(1,0,ULLONG_MAX) == 1);
REQUIRE(fast_power(1,1,ULLONG_MAX) == 1);
Copy link
Member

Choose a reason for hiding this comment

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

ULLONG_MAX should be removed from the tests too. You missed this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Sorry for this.

}

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

TEST_CASE("Overflow cases", "[fast_power]") {
REQUIRE(fast_power(2,100) == 68719476736);
REQUIRE(fast_power(10,99) == 4440381706574496940U);
REQUIRE(fast_power(2,100,1000000007) == 976371285);
REQUIRE(fast_power(10,99,1000000007) == 22673271);
}

#undef FAST_POWER_TEST