In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand the n-th power function of x in big data, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.
1
Title Description
Edit a function that computes x to the nth power. For example: input 2.0000,10, output 9.26100.
2
answer key
Although programming languages have existing exponentiation symbols, this problem requires us to write a function that performs the function ourselves. The time complexity is O(logN), and the time complexity is O(logN). Thinking: Recursive, autonomous algorithms define this function as pow(x,n) if you want to compute 2 to the tenth power (pow(2,10)) is equivalent to computing 2 to the fifth power times 2 to the fifth power (pow(2,5)*pow(2,5)), which in turn is equal to 2 to the second power times 2 to the second power times 2 (pow(2,2)*pow(2,2)*2), and so on. This reduces the time complexity to O(logN). The idea of calculating half each time is similar to dichotomy, which is also a typical algorithm with O(logN) time complexity, so O(logN) and dichotomy should be established. class Solution: def myPow(self, x: float, n: int) -> float: def pow(m): if m==0: return 1.0 else : tmp = pow(m//2) if m%2==0: return tmp*tmp else : return tmp*tmp*x if n>=0: return pow(n) else: return 1.0/pow(-n) Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.