From 860bd1d319474a061a0806dc350b7f4c93a87fa8 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 6 Jul 2017 01:13:56 +0200 Subject: [PATCH 01/10] Added Fast Power --- NumberTheory/FastPower.cpp | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 NumberTheory/FastPower.cpp diff --git a/NumberTheory/FastPower.cpp b/NumberTheory/FastPower.cpp new file mode 100644 index 00000000..40389095 --- /dev/null +++ b/NumberTheory/FastPower.cpp @@ -0,0 +1,47 @@ +/* + +-------------- +Fast Power is an optimized algorithm to compute exponentiation in a short time. + + +Time Complexity +----------------- +O(log(N)) where N is the power the number is raised to. + +Space Complexity +------------------ +O(log(N)) where N is the power the number is raised to. + +*/ +#include + +using namespace std; +typedef long long ll; + +//Function that returns x raised to the power of y +ll fastPower (ll x,ll y) +{ + if (y==0) return 1; + + if (y%2==1) + { + return fastPower(x,y-1) * x; + } + else + { + x = fastPower(x,y/2); + return (x*x); + } +} + +//Testing the function +int main() +{ + int base,power; + cout<<"Enter the number and the power it's raised to:"<>base>>power; + + cout< Date: Thu, 6 Jul 2017 14:20:43 +0200 Subject: [PATCH 02/10] Updated fast Power I have updated the file with the required changes alongside adding sample tests. --- NumberTheory/FastPower.cpp | 66 +++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/NumberTheory/FastPower.cpp b/NumberTheory/FastPower.cpp index 40389095..af2ef166 100644 --- a/NumberTheory/FastPower.cpp +++ b/NumberTheory/FastPower.cpp @@ -1,47 +1,67 @@ /* - --------------- -Fast Power is an optimized algorithm to compute exponentiation in a short time. + 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 power the number is raised to. + Time Complexity + ----------------- + O(log(N)) where N is the exponent. -Space Complexity ------------------- -O(log(N)) where N is the power the number is raised to. + Space Complexity + ----------------- + O(log(N)) where N is the exponent. */ #include using namespace std; -typedef long long ll; +typedef unsigned long long ull; -//Function that returns x raised to the power of y -ll fastPower (ll x,ll y) +//Function that returns base raised to the exponent +ull fast_power (ull base,ull exponent) { - if (y==0) return 1; + if (exponent==0) return 1; - if (y%2==1) + if (exponent%2==1) //If the power is odd { - return fastPower(x,y-1) * x; + return fast_power(base,exponent-1) * base; } - else + else //If the power is even { - x = fastPower(x,y/2); - return (x*x); + base = fast_power(base,exponent/2); + return (base*base); } } -//Testing the function + int main() { - int base,power; - cout<<"Enter the number and the power it's raised to:"<>base>>exponent; + + cout<>base>>power; + TEST_CASE("Normal cases", "[fast_power]") { + REQUIRE(fast_power(3,4) == 81); + REQUIRE(fast_power(7,9) == 40353607); + REQUIRE(fast_power(15,10) == 576650390625); +} - cout< Date: Sat, 8 Jul 2017 00:27:49 +0200 Subject: [PATCH 03/10] Edited fastPower.cpp Removed test cases from the source file. --- NumberTheory/FastPower.cpp | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/NumberTheory/FastPower.cpp b/NumberTheory/FastPower.cpp index af2ef166..7bb471ce 100644 --- a/NumberTheory/FastPower.cpp +++ b/NumberTheory/FastPower.cpp @@ -36,6 +36,7 @@ ull fast_power (ull base,ull exponent) } } +#ifndef FAST_POWER_TEST int main() { @@ -46,22 +47,8 @@ int main() cin>>base>>exponent; cout< Date: Sat, 8 Jul 2017 00:33:25 +0200 Subject: [PATCH 04/10] Added fastPower test file --- Test/NumberTheory/fastPower.test.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Test/NumberTheory/fastPower.test.cpp diff --git a/Test/NumberTheory/fastPower.test.cpp b/Test/NumberTheory/fastPower.test.cpp new file mode 100644 index 00000000..b6f87b4d --- /dev/null +++ b/Test/NumberTheory/fastPower.test.cpp @@ -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); +} + +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); +} + +#undef FAST_POWER_TEST From 98fd22bf2ccd5ef2c91cb15382dc06e0beb50f53 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Sat, 8 Jul 2017 14:36:23 +0200 Subject: [PATCH 05/10] Issues Fixed --- NumberTheory/FastPower.cpp | 13 ++++++++++--- Test/NumberTheory/fastPower.test.cpp | 9 +++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/NumberTheory/FastPower.cpp b/NumberTheory/FastPower.cpp index 7bb471ce..2c32e79b 100644 --- a/NumberTheory/FastPower.cpp +++ b/NumberTheory/FastPower.cpp @@ -41,13 +41,20 @@ ull fast_power (ull base,ull exponent) int main() { //Testing the function - int base,exponent; + ull base,exponent; cout<<"Enter the number and its exponent:"<>base>>exponent; - cout< Date: Sat, 8 Jul 2017 14:38:59 +0200 Subject: [PATCH 06/10] Renamed fastPower.test.cpp to FastPower.test.cpp --- Test/NumberTheory/{fastPower.test.cpp => FastPower.test.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Test/NumberTheory/{fastPower.test.cpp => FastPower.test.cpp} (100%) diff --git a/Test/NumberTheory/fastPower.test.cpp b/Test/NumberTheory/FastPower.test.cpp similarity index 100% rename from Test/NumberTheory/fastPower.test.cpp rename to Test/NumberTheory/FastPower.test.cpp From ea182d52e24f942ffd802b6a779f7c5e3ff0baa0 Mon Sep 17 00:00:00 2001 From: Ahmad Hussain Date: Sat, 8 Jul 2017 15:31:34 +0200 Subject: [PATCH 07/10] Duplicate removed --- Test/NumberTheory/FastPower.test.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Test/NumberTheory/FastPower.test.cpp b/Test/NumberTheory/FastPower.test.cpp index 836c5c51..43613048 100644 --- a/Test/NumberTheory/FastPower.test.cpp +++ b/Test/NumberTheory/FastPower.test.cpp @@ -10,12 +10,11 @@ TEST_CASE("Base cases", "[fast_power]") { 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); } 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); From 7bee107d1ae7ad98c805943b00e738073ef2b9f1 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 20 Jul 2017 01:08:53 +0200 Subject: [PATCH 08/10] Updated FastPower.cpp & FastPower.test.cpp --- NumberTheory/FastPower.cpp | 21 ++++++++++++++++----- Test/NumberTheory/FastPower.test.cpp | 20 ++++++++++---------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/NumberTheory/FastPower.cpp b/NumberTheory/FastPower.cpp index 2c32e79b..209d9893 100644 --- a/NumberTheory/FastPower.cpp +++ b/NumberTheory/FastPower.cpp @@ -16,23 +16,24 @@ */ #include +#include 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) { 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; } } @@ -52,7 +53,17 @@ int main() } else { - cout< 19) + { + cout< Date: Thu, 20 Jul 2017 14:10:10 +0200 Subject: [PATCH 09/10] Updated FastPower.cpp --- NumberTheory/FastPower.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NumberTheory/FastPower.cpp b/NumberTheory/FastPower.cpp index 209d9893..ca7b5e73 100644 --- a/NumberTheory/FastPower.cpp +++ b/NumberTheory/FastPower.cpp @@ -22,7 +22,7 @@ using namespace std; typedef unsigned long long ull; //Function that returns base raised to the exponent -ull fast_power (ull base,ull exponent,ull mod) +ull fast_power (ull base,ull exponent,ull mod = ULLONG_MAX) { if (exponent==0) return 1; @@ -62,7 +62,7 @@ int main() } else { - cout< Date: Thu, 20 Jul 2017 15:11:04 +0200 Subject: [PATCH 10/10] Updated FastPower.test.cpp --- Test/NumberTheory/FastPower.test.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Test/NumberTheory/FastPower.test.cpp b/Test/NumberTheory/FastPower.test.cpp index 0a15fa38..75171f8f 100644 --- a/Test/NumberTheory/FastPower.test.cpp +++ b/Test/NumberTheory/FastPower.test.cpp @@ -7,17 +7,17 @@ TEST_CASE("Base cases", "[fast_power]") { //REQUIRE(fast_power(0,0) == undefined); - REQUIRE(fast_power(0,1,ULLONG_MAX) == 0); - REQUIRE(fast_power(1,0,ULLONG_MAX) == 1); - REQUIRE(fast_power(1,1,ULLONG_MAX) == 1); + REQUIRE(fast_power(0,1) == 0); + REQUIRE(fast_power(1,0) == 1); + REQUIRE(fast_power(1,1) == 1); } TEST_CASE("Normal cases", "[fast_power]") { - 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); + 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); } TEST_CASE("Overflow cases", "[fast_power]") {