[Leetcode series] [algorithm] [simple] effective perfect square (Newton iteration)

topic:

Title link:  https://leetcode-cn.com/problems/valid-perfect-square/

 

Problem-solving ideas:

Method 1: Dichotomy

Find from the half of num, then find the middle number every half

 

Method 2: Newton iteration

As long as it is a continuous function, and find one of the points that meet the requirements, in most cases, you can use Newton iteration

For this problem, the function expression can be understood as:

f(x) = x^2 - num

Where x is the value we want to request and num is the value entered by the program

The iterative idea of ​​Newton's method is as follows:

  1. Pick a value to start iterationx_{1}
  2. Calculating f(x_{1})the tangent slope atf '(x_ {1}) = 2x
  3. Find the slope f'(x_{1}), and (x_ {1}, y_ {1})the straight line passing through the point , the x coordinate of the intersection point with the x axis x_{2}, that is, find the solution to the equation:(x_{2} - x_{1})f'(x_{1}) + f(x_{1}) = 0
    1. x - x_{1}Represents the function of the left and right image translation x_{1}units, high school (middle school?) Knowledge, plus the left and right cut, so that the purpose of the function when the image x - x_{1} = 0, the linear functionf(x) = y_{1}
    2. f'(x_{1})Represents the slope of the tangent equation
    3. x_{2}For the required value this time, the converted expression is:x_{2} = \frac {x_{1}f'{x_{1}} - f(x_{1})}{f'{x_{1}}} = x_{1} - \frac {f(x_{1})}{f'(x_{1})}
    4. For this problem f(x) = x^{2} - num\ ,f'(x) = 2x,, bring the above formula to get:x_{n} = x_{n - 1} - \frac {x^{2}_{n - 1} - num}{2x_{n - 1}}\\\\=\frac {2x^{2}_{n - 1} - x^{2}_{n - 1} + num}{2x_{n - 1}} = \frac {x^{2}_{n - 1} + num}{2x_{n - 1}} = \frac {x_{n - 1}}{2} + \frac {num}{2x_{n - 1}} = \frac {1}{2}(x_{n - 1} + \frac {num}{x_{n - 1}})
  4. Use x_{1}to continue iterating until you find x ^ 2 - num = 0a solution that makes the original formula

Borrowing the wiki animation, the whole process is as follows:

 

Code:

method one:

class Solution:
    def isPerfectSquare(self, num: int) -> bool:
        if num < 2:
            return True
        
        left, right = 0, num // 2
        while left <= right:
            mid = (left + right) // 2
            if mid == 0:
                left = mid + 1
                continue
                
            tmp = num // mid
            mod = num % mid
            if tmp == mid and mod == 0:
                return True
            elif tmp > mid:
                left = mid + 1
            else:
                right = mid - 1
                
        return False

 

Method Two:

class Solution:
    def isPerfectSquare(self, num: int) -> bool:
        if num < 2:
            return True
        
        x = num // 2
        while x * x > num:
            x = (x + num // x) // 2
            
        return x * x == num

 

114 original articles published · Like 11 · Visit 1663

Guess you like

Origin blog.csdn.net/songyuwen0808/article/details/105445567