> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/367_valid_perfect_square.md).

# 367 Valid Perfect Square

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
Returns: True


Example 2:
```

Input: 14 Returns: False

````
```c++
    bool isPerfectSquare(int num) {
        return sqrt(num) == int(sqrt(num));
    }
````

* Assumming I only have to do this once, I would use a for loop to calculate the square. Otherwise, I would calculate all the perfect squares into a hash table, and use this as my look up table.

// too slow

```cpp
int power(int number, int pow) {
    int mult = 1;
    for (int i = 0; i < pow; i++) {
        mult *= number;
    }
    return mult;
}

bool isPerfectSquare(int num) {
    // all powers of 2 are perfect squares
    int cur_power = 1;
    for (int i = 2; i < num; i++) {
        if (num == cur_power) return true;
        else if (cur_power > num) return false;
        cur_power = power(i, 2);
    }
}

int main() {
    cout << boolalpha << isPerfectSquare(1) << endl;
    cout << boolalpha << isPerfectSquare(2) << endl;
    cout << boolalpha << isPerfectSquare(64) << endl;
    cout << boolalpha << isPerfectSquare(2147483647) << endl;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/367_valid_perfect_square.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
