Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-01 16:05:00 10月02日 Update

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.

Tags: Functions complexity complexity time dichotomy recursion data algorithms topics help clarity and so on typical content function can be passed that is thought thinking. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Apple Shulou Tech Info Linux