From 68c93d8efb27b9cb3e75b5e6dfae41b3252985e7 Mon Sep 17 00:00:00 2001 From: Space Date: Sun, 21 Oct 2018 22:55:50 +0530 Subject: [PATCH 1/2] Added new Algorithms to compute power for such numbers whose answer is too big(For instance 1 followed by thousand zeroes). Using old multiplicatiion method will render invalid result for such huge numbers(To be presise one would get 0 in case of C++14). --- Math/Power_For_Huge_Num/Power_Huge.cpp | 86 ++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Math/Power_For_Huge_Num/Power_Huge.cpp diff --git a/Math/Power_For_Huge_Num/Power_Huge.cpp b/Math/Power_For_Huge_Num/Power_Huge.cpp new file mode 100644 index 000000000..ecfb5ffb5 --- /dev/null +++ b/Math/Power_For_Huge_Num/Power_Huge.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +// Maximum number of digits in output +// x^n where 1 <= x, n <= 10000 and overflow may happen +#define MAX 100000 + +// This function multiplies x +// with the number represented by res[]. +// res_size is size of res[] or +// number of digits in the number +// represented by res[]. This function +// uses simple school mathematics +// for multiplication. +// This function may value of res_size +// and returns the new value of res_size +int multiply(int x, int res[], int res_size) { + +// Initialize carry +int carry = 0; + +// One by one multiply n with +// individual digits of res[] +for (int i = 0; i < res_size; i++) { + int prod = res[i] * x + carry; + + // Store last digit of + // 'prod' in res[] + res[i] = prod % 10; + + // Put rest in carry + carry = prod / 10; +} + +// Put carry in res and +// increase result size +while (carry) { + res[res_size] = carry % 10; + carry = carry / 10; + res_size++; +} +return res_size; +} + +// This function finds +// power of a number x +void power(int x, int n) +{ + +//printing value "1" for power = 0 +if(n == 0 ){ + cout<<"1"; + return; +} + + +int res[MAX]; +int res_size = 0; +int temp = x; + +// Initialize result +while (temp != 0) { + res[res_size++] = temp % 10; + temp = temp / 10; +} + +// Multiply x n times +// (x^n = x*x*x....n times) +for (int i = 2; i <= n; i++) + res_size = multiply(x, res, res_size); + +cout << x << "^" << n << " = "; +for (int i = res_size - 1; i >= 0; i--) + cout << res[i]; +} + +// Driver program +int main() { +int exponent, base; +printf("Enter base "); +scanf("%id \n", &base); +printf("Enter exponent "); +scanf("%id", &exponent); +power(base, exponent); +return 0; +} From 62ff5e5d6130d379396d783ba6fc3059b72aeae7 Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Wed, 13 Feb 2019 19:28:19 +0530 Subject: [PATCH 2/2] Indent code --- Math/Power_For_Huge_Num/Power_Huge.cpp | 140 ++++++++++++------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/Math/Power_For_Huge_Num/Power_Huge.cpp b/Math/Power_For_Huge_Num/Power_Huge.cpp index ecfb5ffb5..02f01dc50 100644 --- a/Math/Power_For_Huge_Num/Power_Huge.cpp +++ b/Math/Power_For_Huge_Num/Power_Huge.cpp @@ -1,86 +1,86 @@ -#include -using namespace std; +#include +using namespace std; -// Maximum number of digits in output +// Maximum number of digits in output // x^n where 1 <= x, n <= 10000 and overflow may happen -#define MAX 100000 +#define MAX 100000 -// This function multiplies x -// with the number represented by res[]. -// res_size is size of res[] or -// number of digits in the number -// represented by res[]. This function -// uses simple school mathematics -// for multiplication. -// This function may value of res_size -// and returns the new value of res_size -int multiply(int x, int res[], int res_size) { +// This function multiplies x +// with the number represented by res[]. +// res_size is size of res[] or +// number of digits in the number +// represented by res[]. This function +// uses simple school mathematics +// for multiplication. +// This function may value of res_size +// and returns the new value of res_size +int multiply(int x, int res[], int res_size) { -// Initialize carry -int carry = 0; + // Initialize carry + int carry = 0; -// One by one multiply n with -// individual digits of res[] -for (int i = 0; i < res_size; i++) { - int prod = res[i] * x + carry; + // One by one multiply n with + // individual digits of res[] + for (int i = 0; i < res_size; i++) { + int prod = res[i] * x + carry; - // Store last digit of - // 'prod' in res[] - res[i] = prod % 10; + // Store last digit of + // 'prod' in res[] + res[i] = prod % 10; - // Put rest in carry - carry = prod / 10; -} + // Put rest in carry + carry = prod / 10; + } -// Put carry in res and -// increase result size -while (carry) { - res[res_size] = carry % 10; - carry = carry / 10; - res_size++; -} -return res_size; -} + // Put carry in res and + // increase result size + while (carry) { + res[res_size] = carry % 10; + carry = carry / 10; + res_size++; + } + return res_size; +} -// This function finds -// power of a number x -void power(int x, int n) -{ +// This function finds +// power of a number x +void power(int x, int n) +{ -//printing value "1" for power = 0 -if(n == 0 ){ - cout<<"1"; - return; -} + //printing value "1" for power = 0 + if(n == 0 ){ + cout<<"1"; + return; + } -int res[MAX]; -int res_size = 0; -int temp = x; + int res[MAX]; + int res_size = 0; + int temp = x; -// Initialize result -while (temp != 0) { - res[res_size++] = temp % 10; - temp = temp / 10; -} + // Initialize result + while (temp != 0) { + res[res_size++] = temp % 10; + temp = temp / 10; + } -// Multiply x n times -// (x^n = x*x*x....n times) -for (int i = 2; i <= n; i++) - res_size = multiply(x, res, res_size); + // Multiply x n times + // (x^n = x*x*x....n times) + for (int i = 2; i <= n; i++) + res_size = multiply(x, res, res_size); -cout << x << "^" << n << " = "; -for (int i = res_size - 1; i >= 0; i--) - cout << res[i]; -} + cout << x << "^" << n << " = "; + for (int i = res_size - 1; i >= 0; i--) + cout << res[i]; +} -// Driver program -int main() { -int exponent, base; -printf("Enter base "); -scanf("%id \n", &base); -printf("Enter exponent "); -scanf("%id", &exponent); -power(base, exponent); -return 0; -} +// Driver program +int main() { + int exponent, base; + printf("Enter base "); + scanf("%id \n", &base); + printf("Enter exponent "); + scanf("%id", &exponent); + power(base, exponent); + return 0; +}