-
Notifications
You must be signed in to change notification settings - Fork 363
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
Changes from 5 commits
860bd1d
546bc5b
348ad07
60b4356
e0d5338
98fd22b
f61dcfd
ea182d5
7bee107
e90a43e
c4255a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cout<<"Enter the number and its exponent:"<<endl; | ||
|
||
cin>>base>>exponent; | ||
|
||
cout<<endl<<fast_power(base,exponent)<<endl; | ||
|
||
return 0; | ||
} | ||
|
||
#endif |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under "Base cases", check the values of the following:
|
||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I meant I don't think you need to output modulo a specific number. The result will automatically be modulo No, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha! Sorry for the misunderstanding. |
||
} | ||
|
||
#undef FAST_POWER_TEST |
There was a problem hiding this comment.
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.