#include <stdio.h>
//this gives you the power in a time of O(log N)
//for numbers that may go out of range of int...use "long long int" in place of "int"
//for understanding the program logic even better you can visit...http://w...content-available-to-author-only...c.com/fast-exponentiation-algorithms/
int exp_by_squaring
(int n
,int pow)//this is a recursive solution...an iterative solution is also possible...!!! {
if(pow==0)//power is zero return 1;
return n;
if(pow&1)//pow is odd
{
int ans
=exp_by_squaring
(n
,pow/2); return ans*ans*n;
}
//pow is even
int ans
=exp_by_squaring
(n
,pow/2); return ans*ans;
}
int main(void)
{
printf("%d",exp_by_squaring
(2,10)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgovL3RoaXMgZ2l2ZXMgeW91IHRoZSBwb3dlciBpbiBhIHRpbWUgb2YgTyhsb2cgTikKCi8vZm9yIG51bWJlcnMgdGhhdCBtYXkgZ28gb3V0IG9mIHJhbmdlIG9mIGludC4uLnVzZSAibG9uZyBsb25nIGludCIgaW4gcGxhY2Ugb2YgImludCIKCi8vZm9yIHVuZGVyc3RhbmRpbmcgdGhlIHByb2dyYW0gbG9naWMgZXZlbiBiZXR0ZXIgeW91IGNhbiB2aXNpdC4uLmh0dHA6Ly93Li4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5jLmNvbS9mYXN0LWV4cG9uZW50aWF0aW9uLWFsZ29yaXRobXMvCgppbnQgZXhwX2J5X3NxdWFyaW5nKGludCBuLGludCBwb3cpLy90aGlzIGlzIGEgcmVjdXJzaXZlIHNvbHV0aW9uLi4uYW4gaXRlcmF0aXZlIHNvbHV0aW9uIGlzIGFsc28gcG9zc2libGUuLi4hISEKewogICAgaWYocG93PT0wKS8vcG93ZXIgaXMgemVybwogICAgICAgIHJldHVybiAxOwogICAgaWYocG93PT0xKS8vcG93ZXIgaXMgMQogICAgICAgIHJldHVybiBuOwogICAgaWYocG93JjEpLy9wb3cgaXMgb2RkCiAgICB7CiAgICAgICAgaW50IGFucz1leHBfYnlfc3F1YXJpbmcobixwb3cvMik7CiAgICAgICAgcmV0dXJuIGFucyphbnMqbjsKICAgIH0KICAgIC8vcG93IGlzIGV2ZW4KICAgIGludCBhbnM9ZXhwX2J5X3NxdWFyaW5nKG4scG93LzIpOwogICAgcmV0dXJuIGFucyphbnM7Cn0KaW50IG1haW4odm9pZCkKewogICAgcHJpbnRmKCIlZCIsZXhwX2J5X3NxdWFyaW5nKDIsMTApKTsKCXJldHVybiAwOwp9