Power Operator and Perfect Squares

Easy ⏱ 8 min 78% acceptance ★★★★☆ 4.4
Write a function is_perfect_square(n) that returns True if the non-negative integer n is a perfect square, using the ** (power) operator to test candidates rather than importing math.sqrt.

Examples

Example 1
Input
is_perfect_square(16)
Output
True
Explanation

4 ** 2 == 16.

Example 2
Input
is_perfect_square(15)
Output
False
Explanation

No integer squared equals 15.

Constraints

  • n is a non-negative integer.
  • Do not use math.sqrt or math.isqrt.

Topics

Operatorsexponentiation

Companies

MetaAdobe

Hints

Hint 1

Compute an approximate root with `n ** 0.5`, round it, then verify with **.

Hint 2

Check both the rounded-down and rounded-up integer candidates to avoid float rounding errors.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.