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
54 changes: 54 additions & 0 deletions NumberTheory/FastPower.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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
int base,exponent;
Copy link
Member

Choose a reason for hiding this comment

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

int ull

cout<<"Enter the number and its exponent:"<<endl;

cin>>base>>exponent;

cout<<endl<<fast_power(base,exponent)<<endl;

return 0;
}

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

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


TEST_CASE("Base cases", "[fast_power]") {
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.

Under "Base cases", check the values of the following:

  • fast_power(0, 0) is undefined or NaN. Comment this case here and handle it properly in the code.
  • fast_power(0, 1) should be 0
  • fast_power(1, 0) should be 1
  • fast_power(1, 1) should be 1

}

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

@faheel faheel Jul 8, 2017

Choose a reason for hiding this comment

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

fast_power(2, 100) should be 68719476736 (see this) and fast_power(10, 99) should be 4440381706574496940U (see this). Suffix U is for unsigned.

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 think you meant fast_power(2 ,100) in the first one.
Anyway, regarding large numbers, to fix the output being 0 we can output them modulo a large number such as 1000000000007 , but not a huge number like 2^64. Where fast_power(2, 100) will be 40955801449 .
Regarding the last statement, do you mean changing ull to Ull ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I meant fast_power(2, 100) in the first one. Fixed it.

I don't think you need to output modulo a specific number. The result will automatically be modulo MAX_ULL, which is 264-1.

No, ull is fine. I meant you need to add the suffix U to unsigned numbers greater than 263-1 to prevent warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha! Sorry for the misunderstanding.

}

#undef FAST_POWER_TEST