In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to implement an exponential operation with python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to use python to implement an exponential operation"!
This problem is very simple, is to implement an exponential operation method, but for the number of recursions and operation time constraints.
Exponential operation, that is pow(x,n), x can be floating point number, input x is 2.000, n is 10, return 1024.000; where exponent n can be negative.
Input: 2.00000, 10
Output: 1024.00000
In fact, Python default built-in exponent calculation symbol is **, for example, the above is 2.000**10; directly using return x**n submitted unexpectedly also passed, but also faster than 62% of the time.
Think about it, of course, not so much, think about it, directly calculating a product must exceed the computational efficiency constraint; you can use a dichotomy, such as pow(x,n), if n is even, it can be divided into pow(x,n/2)*pow(x,n/2); if n is not even, it is multiplied by x, and the rest is even. Then use the cache dictionary method to avoid double counting.
In later calculations, it is found that the test system does not create a new object to refresh the cache dictionary every time it calculates a different (x, n), causing the second (x,n) to use the first cache dictionary; therefore, the dictionary is cached every time a new (x, n) is calculated.
After submission, it was found that 87% of the submitted answers were faster. Compared to Python's built-in n**x, it's also faster.
The code is as follows:
class Solution: cacheDict = {} def myRecursion(self,x,n): if n in self.cacheDict.keys(): return self.cacheDict[n] if n%2 == 1: productRe = self.myRecursion(x, (n-1)/2)*self.myRecursion(x, (n-1)/2)*x else: productRe = self.myRecursion(x, n/2)*self.myRecursion(x, n/2) self.cacheDict[n] = productRe return productRe def myPow(self, x, n) -> float: if n < 0: self.cacheDict = {0:1,1:1/x} return self.myRecursion(1.0/x, abs(n)) else: self.cacheDict = {0:1,1:x} return self.myRecursion(x, n) At this point, I believe that everyone has a deeper understanding of "how to use python to implement an exponential operation", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.