{"id":25535,"date":"2017-05-30T18:59:26","date_gmt":"2017-05-30T13:29:26","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25535"},"modified":"2017-05-30T18:59:26","modified_gmt":"2017-05-30T13:29:26","slug":"write-a-program-to-calculate-pow-xn","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-a-program-to-calculate-pow-xn\/","title":{"rendered":"Write a program to calculate pow (x,n)"},"content":{"rendered":"<p><strong>Below solution divides the problem into subproblems of size y\/2 and call the subproblems <a href=\"https:\/\/www.wikitechy.com\/technology\/introduction-for-divide-and-conquer\/\">recursively<\/a>.<\/strong><\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/> <br\/>\/* Function to calculate x raised to the power y *\/<br\/>int power(int x, unsigned int y)<br\/>{<br\/>    if (y == 0)<br\/>        return 1;<br\/>    else if (y%2 == 0)<br\/>        return power(x, y\/2)*power(x, y\/2);<br\/>    else<br\/>        return x*power(x, y\/2)*power(x, y\/2);<br\/>}<br\/> <br\/>\/* Program to test function power *\/<br\/>int main()<br\/>{<br\/>    int x = 2;<br\/>    unsigned int y = 3;<br\/> <br\/>    printf(&quot;%d&quot;, power(x, y));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Time Complexity: <\/strong>O(n)<br \/>\n<strong>Space Complexity:<\/strong> O(1)<br \/>\n<strong>Algorithmic Paradigm: <\/strong>Divide and conquer.<\/p>\n<p>Above function can be optimized to O(logn) by calculating power(x, y\/2) only once and storing it.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* Function to calculate x raised to the power y in O(logn)*\/<br\/>int power(int x, unsigned int y)<br\/>{<br\/>    int temp;<br\/>    if( y == 0)<br\/>        return 1;<br\/>    temp = power(x, y\/2);<br\/>    if (y%2 == 0)<br\/>        return temp*temp;<br\/>    else<br\/>        return x*temp*temp;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Time Complexity of optimized solution:<\/strong> O(logn)<br \/>\nLet us extend the pow function to work for negative y and float x.<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* Extended version of power function that can work<br\/> for float x and negative y*\/<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>float power(float x, int y)<br\/>{<br\/>    float temp;<br\/>    if( y == 0)<br\/>       return 1;<br\/>    temp = power(x, y\/2);       <br\/>    if (y%2 == 0)<br\/>        return temp*temp;<br\/>    else<br\/>    {<br\/>        if(y &gt; 0)<br\/>            return x*temp*temp;<br\/>        else<br\/>            return (temp*temp)\/x;<br\/>    }<br\/>}  <br\/> <br\/>\/* Program to test function power *\/<br\/>int main()<br\/>{<br\/>    float x = 2;<br\/>    int y = -3;<br\/>    printf(&quot;%f&quot;, power(x, y));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a program to calculate pow(x,n) &#8211; Divide and Conquer &#8211; Above function can be optimized to O(logn) by calculating power(x, y\/2) only once and storing it.<\/p>\n","protected":false},"author":1,"featured_media":26073,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73719],"tags":[73765,73766,73773,73778,73780,73776,73768,73767,73770,73771,73772,73769,73774,73779,73777,73764],"class_list":["post-25535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-divide-and-conquer","tag-c-program-to-calculate-power-of-a-number","tag-c-program-to-calculate-power-of-a-number-using-recursion","tag-c-program-to-find-power-of-a-number-using-for-loop","tag-c-program-to-find-power-of-a-number-using-function","tag-c-program-to-find-power-of-a-number-without-using-pow-function","tag-c-programming-power-function","tag-calculate-power-of-a-number","tag-how-to-calculate-power","tag-how-to-calculate-power-in-c","tag-how-to-calculate-power-number","tag-how-to-calculate-power-of-2","tag-how-to-calculate-power-of-a-number","tag-implement-powx","tag-implement-power-function-leetcode","tag-implement-power-function-using-binary-search","tag-write-a-program-to-calculate-powx"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25535"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/26073"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}