You are on page 1of 11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

GeeksforGeeks
A computer science portal for geeks Home Q&A Interview Corner Ask a question Feedback Contribute About us Subscribe Arrays Articles Bit Magic C/C++ Puzzles GFacts Linked Lists MCQ Misc Output Strings Trees

Write a C program to calculate pow(x,n)


March 22, 2009 Below solution divides the problem into subproblems of size y/2 and call the subproblems recursively. #nld<ti.> icuesdoh / Fnto t cluaexrie t tepwry* * ucin o aclt asd o h oe / itpwritx usge ity n oe(n , nind n ) { i(y= 0 f = ) rtr 1 eun ; es i (% = 0 le f y2 = ) rtr pwrx y2*oe(,y2; eun oe(, /)pwrx /)
www.geeksforgeeks.org/archives/28 1/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

es le rtr xpwrx y2*oe(,y2; eun *oe(, /)pwrx /) } / Pormt ts fnto pwr* * rga o et ucin oe / itmi( n an) { itx=2 n ; usge ity=3 nind n ; pit(%" pwrx y) rnf"d, oe(, ); gthr) eca(; rtr 0 eun ;

Time Complexity: O(n) Space Complexity: O(1) Algorithmic Paradigm: Divide and conquer. Above function can be optimized to O(logn) by calculating power(x, y/2) only once and storing it. / Fnto t cluaexrie t tepwryi Olg)/ * ucin o aclt asd o h oe n (on* itpwritx usge ity n oe(n , nind n ) { ittm; n ep i(y= 0 f = ) rtr 1 eun ; tm =pwrx y2; ep oe(, /) i (% = 0 f y2 = ) rtr tm*ep eun eptm; es le rtr xtm*ep eun *eptm; } Time Complexity of optimized solution: O(logn) Let us extend the pow function to work for negative y and float x. / Etne vrino pwrfnto ta cnwr * xedd eso f oe ucin ht a ok frfotxadngtv y/ o la n eaie * #nld<ti.> icuesdoh fotpwrfotx ity la oe(la , n ) { fottm; la ep i(y= 0 f = ) rtr 1 eun ;
www.geeksforgeeks.org/archives/28 2/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

tm =pwrx y2; ep oe(, /) i (% = 0 f y2 = ) rtr tm*ep eun eptm; es le { i( >0 fy ) rtr xtm*ep eun *eptm; es le rtr (eptm)x eun tm*ep/; }

/ Pormt ts fnto pwr* * rga o et ucin oe / itmi( n an) { fotx=2 la ; ity=-; n 3 pit(%" pwrx y) rnf"f, oe(, ); gthr) eca(; rtr 0 eun ; }
Send 6 people

No related posts.

12 comments so far
1. Anuj says: February 5, 2011 at 12:58 PM #nld<ti.> icuesdoh mi( an) { itnm p,rsl; n u, eut pit(Etrtenme :"; rnf"ne h ubr ) saf"d,nm; cn(%"&u) pit(\Adispwras.:"; rnf"nn t oe lo ) saf"d,p; cn(%"&)
www.geeksforgeeks.org/archives/28 3/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

} pwritx ity oe(n , n ) { ititm =; n ,ep 1 i(=0 fy=) rtr() eun1; i(=1 fy=) rtr() eunx; es le { fri1i=;+) o(=;<yi+ tm =tm*; ep epx rtr(ep; euntm) } } Reply Anuj says: February 5, 2011 at 12:59 PM I think it's pretty simple Reply KK123 says: July 6, 2011 at 12:32 PM Dude!! Din u notice time complexity?? Reply Anuj says: July 6, 2011 at 3:30 PM i didn't. u tell me Reply KK123 says: July 6, 2011 at 7:38 PM

rsl =pwrnmp; eut oe(u,) pit(\Tersl i %\" rsl) rnf"nh eut s dn, eut;

ur algo takes O(n) whereas the one in article is O(logn)... 2. Raj says: January 11, 2011 at 4:09 PM How to calculate power(float x, float y) ?
www.geeksforgeeks.org/archives/28 4/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

Reply 3. Snehal Masne says: December 20, 2010 at 7:44 PM Thanks for the code.. interesting.. Reply 4. Venki says: August 27, 2010 at 11:54 PM / Smlrfnto,btdpnso stbt / iia ucin u eed n e is / i teepnn (orcrin-lg) / n h xoet n euso oN itpwritx usge n { n oe(n , nind ) / Hlsnx pwr / od et oe / Wl b ue i nx bti st(d) / il e sd f et i s e od ititreitPout=x n nemdaerdc ; / Fnlrsl / ia eut itrsl =1 n eut ;

/ Rpa ti tl w ieaealtestbt / eet hs il e trt l h e is wien hl() { / Efcieyeuaett n2 / fetvl qvln o % i ( &1 f n ) { / I ni od / f s d / pwra b =a*pwra b2 *pwra b2 / oe(, ) oe(, /) oe(, /) / Nt ta nx totrsaepce i tedw sae / oe ht et w em r ikd n h on tt rsl =itreitPout*rsl; eut nemdaerdc eut } / Mk u nx bt / ae p et i n>=1 > ;

/ Mk temlilcto frnx bt / ae h utpiain o et i / Cnrbtsfrmlilcto we ni ee / otiue o utpiain hn s vn / pwra b =pwra b2 *pwra b2 / oe(, ) oe(, /) oe(, /) itreitPout=itreitPout*itreitPo nemdaerdc nemdaerdc nemdaerd

/ Dn!Rtr. / oe eun rtr rsl; eun eut

/ Die / rvr vi mi( od an) {


www.geeksforgeeks.org/archives/28 5/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

pit(%\" pwr2 5) rnf"dn, oe(, );

Reply 5. Prabhakar says: August 17, 2010 at 2:03 AM we have to calculate x^n; itn n ; wien hl() { i(&1 fn&) xxy =*; yyy =*; nn>; =>1 } Time Complexity=O(log(n)) Reply codegeek says: August 27, 2010 at 6:31 PM why if (n&&1) its same as if (n) what is initial value of y here?? Reply Prabhakar says: August 27, 2010 at 7:08 PM @codegeeks sorry. It is (n&1) not (n&&1). here intialise x=1,y=n; Reply nikhil says: September 2, 2010 at 6:43 PM @Prabhakar In the above initialisation we are not using value of anywhere in the
www.geeksforgeeks.org/archives/28 6/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

expression x^n. since x=1 and y=n and n=n . so how value of x will be used. I hope u understand my question. Reply

Comment
Name (Required) Website URI code between sourcecode tags) Email (Required) Your Comment (Writing code? please paste your

[oreoelnug=C] succd agae"" / Pseyu cd hr (o mydlt teelns * at or oe ee Yu a eee hs ie i ntwiigcd)* f o rtn oe / [succd] /oreoe

Have Your Say

Pingbacks/Trackbacks
1. Program for Fibonacci numbers | GeeksforGeeks

Search

Popular Tags
GATE Java Dynamic Programming Backtracking Pattern Searching Divide & Conquer Graph
www.geeksforgeeks.org/archives/28 7/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

Operating Systems Recursion

Popular Posts
All permutations of a given string Memory Layout of C Programs Understanding extern keyword in C Median of two sorted arrays Tree traversal without recursion and without stack! Structure Member Alignment, Padding and Data Packing Intersection point of two Linked Lists Lowest Common Ancestor in a BST. Check if a binary tree is BST or not Sorted Linked List to Balanced BST

www.geeksforgeeks.org/archives/28

8/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

250

Subscribe

Forum Latest Discussion


LIS in nlogn time
Last Post By: kartik Inside: Interview Questions

tree to file
Last Post By: Dheeraj Inside: Interview Questions
www.geeksforgeeks.org/archives/28 9/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

Count internal nodes in a binary tree


Last Post By: kartik Inside: Interview Questions

Ancestor of two given leaf nodes


Last Post By: dead Inside: Trees specific questions

combine elements of an array, so as to minimize weights.


Last Post By: rohanag Inside: Interview Questions

FB interview question
Last Post By: ranganath111 Inside: Interview Questions

Testing of factorial of a number


Last Post By: rukawa Inside: Interview Questions

numeric puzzle
Last Post By: asm Inside: Algorithms

Forum Categories
Interview Questions C/C++ Programming Questions Algorithms Trees specific questions Linked List specific questions Multiple Choice Questions Object oriented queries GPuzzles
www.geeksforgeeks.org/archives/28 10/11

5/12/12

Write a C program to calculate pow(x,n) | GeeksforGeeks

Operating Systems Miscellaneous Java specific Questions Perl specific Questions

Recent Comments
Venki on Structure Member Alignment, Padding and Data Packing avi on Structure Member Alignment, Padding and Data Packing atul on A Program to check if strings are rotations of each other or not Venki on Structure Member Alignment, Padding and Data Packing rahul on Dynamic Programming | Set 13 (Cutting a Rod) Sandeep on Dynamic Programming | Set 3 (Longest Increasing Subsequence) Yueming on Dynamic Programming | Set 3 (Longest Increasing Subsequence) avi on Structure Member Alignment, Padding and Data Packing @geeksforgeeks, Some rights reserved Powered by WordPress & MooTools, customized by geeksforgeeks team

www.geeksforgeeks.org/archives/28

11/11

You might also like