Sqrt(x) - Newton's Method

EasyTree

Description

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. Use Newton's method for calculation.

Examples

Input:x = 8
Output:2
Explanation:

Newton's method iterates: start with guess 4, compute (4 + 8/4)/2 = 3, then (3 + 8/3)/2 = 2.83..., converging to 2.82. Floor gives 2.

Input:x = 0
Output:0
Explanation:

√0 = 0 exactly, so the result is 0. This tests the edge case of the minimum possible input.

Input:x = 15
Output:3
Explanation:

√15 = 3.872..., rounded down to 3. This demonstrates rounding down when the square root is not a perfect integer.

Constraints

  • 0 ≤ x ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!