[algogeeks] An Interesting Question Calculate nth Power of Integer
How you will print the 100th power of a single digit( which is of type int). How do you maintain that big number in memory? Lets C The Approach Thank Regards Shashank -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] An Interesting Question Calculate nth Power of Integer
Try this... #include iostream #include cmath using namespace std; #define DIGITS 10001 void mult(int N,int pro[],int len) { int carry = 0; for(int i=0;ilen;i++) { int temp = pro[i]*N + carry; pro[i] = temp%10; carry = temp/10; } if(carry0) { pro[len] = carry; len++; } } int main() { int t,N,E; scanf(%d,t); while(t--) { int pro[DIGITS]; scanf(%d %d,N,E); if(N==1) { printf(1 1\n); continue; } pro[0] = 1; int len = 1; for(int i=0;iE;i++) { mult(N,pro,len); } for(int i=len-1;i=0;i--) { printf(%d,pro[i]); } printf(\n); } return 0; } Here t stands for number of testcases... N = the number for which power is to be calculated.. E = the exponent.. On Thu, Mar 24, 2011 at 12:22 PM, bittu shashank7andr...@gmail.com wrote: How you will print the 100th power of a single digit( which is of type int). How do you maintain that big number in memory? Lets C The Approach Thank Regards Shashank -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] An Interesting Question Calculate nth Power of Integer
You can do it in (log n) assuming multiplication is O(1) suppose u are going to calculate 8 power 33 u compute 8 power 16 and multiply with the same to get 8 power 32 then multiply with 8 to get the result On Thu, Mar 24, 2011 at 1:04 PM, AAMIR KHAN ak4u2...@gmail.com wrote: Try this... #include iostream #include cmath using namespace std; #define DIGITS 10001 void mult(int N,int pro[],int len) { int carry = 0; for(int i=0;ilen;i++) { int temp = pro[i]*N + carry; pro[i] = temp%10; carry = temp/10; } if(carry0) { pro[len] = carry; len++; } } int main() { int t,N,E; scanf(%d,t); while(t--) { int pro[DIGITS]; scanf(%d %d,N,E); if(N==1) { printf(1 1\n); continue; } pro[0] = 1; int len = 1; for(int i=0;iE;i++) { mult(N,pro,len); } for(int i=len-1;i=0;i--) { printf(%d,pro[i]); } printf(\n); } return 0; } Here t stands for number of testcases... N = the number for which power is to be calculated.. E = the exponent.. On Thu, Mar 24, 2011 at 12:22 PM, bittu shashank7andr...@gmail.com wrote: How you will print the 100th power of a single digit( which is of type int). How do you maintain that big number in memory? Lets C The Approach Thank Regards Shashank -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] An Interesting Question Calculate nth Power of Integer
Yeah i know the algorithm is not very much efficient...But i was trying to show how to tackle with big integers in C++ (i.e, using arrays) On Thu, Mar 24, 2011 at 1:06 PM, radha krishnan radhakrishnance...@gmail.com wrote: You can do it in (log n) assuming multiplication is O(1) suppose u are going to calculate 8 power 33 u compute 8 power 16 and multiply with the same to get 8 power 32 then multiply with 8 to get the result On Thu, Mar 24, 2011 at 1:04 PM, AAMIR KHAN ak4u2...@gmail.com wrote: Try this... #include iostream #include cmath using namespace std; #define DIGITS 10001 void mult(int N,int pro[],int len) { int carry = 0; for(int i=0;ilen;i++) { int temp = pro[i]*N + carry; pro[i] = temp%10; carry = temp/10; } if(carry0) { pro[len] = carry; len++; } } int main() { int t,N,E; scanf(%d,t); while(t--) { int pro[DIGITS]; scanf(%d %d,N,E); if(N==1) { printf(1 1\n); continue; } pro[0] = 1; int len = 1; for(int i=0;iE;i++) { mult(N,pro,len); } for(int i=len-1;i=0;i--) { printf(%d,pro[i]); } printf(\n); } return 0; } Here t stands for number of testcases... N = the number for which power is to be calculated.. E = the exponent.. On Thu, Mar 24, 2011 at 12:22 PM, bittu shashank7andr...@gmail.com wrote: How you will print the 100th power of a single digit( which is of type int). How do you maintain that big number in memory? Lets C The Approach Thank Regards Shashank -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.