# leetcode\_367

## Given a positive integer num, write a function which returns True if num is a perfect square else False.

## Note: Do not use any built-in library function such as sqrt.

```
Example 1:

Input: 16
Output: true

Example 2:

Input: 14
Output: false
```

## Solutions

* The problem is equivalent to calculating the square root of the given number.
* See problem 69 for details.
* **newton's method**

```cpp
class Solution {
public:
    bool isPerfectSquare(int num) {
        long root =  num;
        while (root * root > num) {
            root = (root + num / root) / 2;
        }
        return root * root == num;
    }
};
```

1. **binary search**

```cpp
class Solution {
public:
    bool isPerfectSquare(int num) {
        int lo = 1, hi = num;
        while (lo < hi) {
            long mid = lo + ((hi - lo) >> 1);
            if (mid * mid < num)
                lo = mid + 1;
            else
                hi = mid;
        }
        return (long)lo * lo == num;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhongquan789.gitbook.io/leetcode/leetcode/leetcode_367.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
