Computing power

Ok so your interviewer asks you write a function to find the power of a number. We have all used Math.pow() in JAVA, pow() function in C/C++, but have you wondered how to compute the power without using these????

Let’s find out.

Example:
x = 2
y = 3…


This content originally appeared on DEV Community and was authored by hebaShakeel

Ok so your interviewer asks you write a function to find the power of a number. We have all used Math.pow() in JAVA, pow() function in C/C++, but have you wondered how to compute the power without using these????

Let's find out.

Example:
x = 2
y = 3
Output = 8 ( 2 to the power 3 = 2 *2 *2)

Naive Solution: Time Complexity = O(n)
Image description

Output:
Image description

Recursive Solution:
Time Complexity = θ(logn)
Space Complexity = θ(logn)
Image description

Output:
Image description

It has recursive function calls which will take space in function call stack. It also has overhead for function call in return.

Can you think of a better solution?
How about an Iterative solution?
Do share your views in the comments.


This content originally appeared on DEV Community and was authored by hebaShakeel


Print Share Comment Cite Upload Translate Updates
APA

hebaShakeel | Sciencx (2022-06-17T18:33:44+00:00) Computing power. Retrieved from https://www.scien.cx/2022/06/17/computing-power/

MLA
" » Computing power." hebaShakeel | Sciencx - Friday June 17, 2022, https://www.scien.cx/2022/06/17/computing-power/
HARVARD
hebaShakeel | Sciencx Friday June 17, 2022 » Computing power., viewed ,<https://www.scien.cx/2022/06/17/computing-power/>
VANCOUVER
hebaShakeel | Sciencx - » Computing power. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/17/computing-power/
CHICAGO
" » Computing power." hebaShakeel | Sciencx - Accessed . https://www.scien.cx/2022/06/17/computing-power/
IEEE
" » Computing power." hebaShakeel | Sciencx [Online]. Available: https://www.scien.cx/2022/06/17/computing-power/. [Accessed: ]
rf:citation
» Computing power | hebaShakeel | Sciencx | https://www.scien.cx/2022/06/17/computing-power/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.