EasyMathBinary Search

2 approaches, code in every language

Problem description

Given a positive integer, decide whether it is a perfect square, meaning it equals some whole number multiplied by itself. Return true when such a whole number exists and false otherwise.

The catch is that built-in square-root helpers are not allowed, so you must determine the answer using arithmetic and a search of your own.

Example 1

Input: num = 16

Output: true

16 is 4 times 4, so it is a perfect square.

Example 2

Input: num = 14

Output: false

No whole number squared gives 14, so the answer is false.

Constraints

  • 1 ≤ num ≤ 2^31 - 1
  • You may not use any built-in square root function

After the pattern

Now solve one you haven't seen.

Square Crate Block is our own problem on the technique behind Valid Perfect Square: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free