Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the n-power function of x in big data

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report