An Interest In:
Web News this Week
- March 6, 2024
- March 5, 2024
- March 4, 2024
- March 3, 2024
- March 2, 2024
- March 1, 2024
- February 29, 2024
June 17, 2022 06:33 pm GMT
Original Link: https://dev.to/hebashakeel/computing-power-53fm
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
Output = 8 ( 2 to the power 3 = 2 *2 *2)
Naive Solution: Time Complexity = O(n)
Recursive Solution:
Time Complexity = (logn)
Space Complexity = (logn)
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.
Original Link: https://dev.to/hebashakeel/computing-power-53fm
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To