Skip to content

Commit 6816b22

Browse files
committed
Add solution of SquareRoot task using Babylon method
1 parent 977d6f2 commit 6816b22

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ they contain enough code which describes implementation in a natural way.
249249
| Java интервью 11 | [Youtube](https://youtu.be/zufbVgdBCAI) | - |
250250
| Java интервью 12 | [Youtube](https://youtu.be/PD41epg_pT4) | - |
251251
| Функциональные тесты REST API с помощью Spock | [Youtube](https://youtu.be/GK5y3oA3qfM) | - |
252+
| Вычисление квадратного корня вавилонским методом (leetcode) | [Youtube](https://youtu.be/41zAzebmOuc) | [Code](src/main/java/by/andd3dfx/numeric/SquareRootBabylon.java) |
252253

253254
## Materials & notes
254255

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package by.andd3dfx.numeric;
2+
3+
/**
4+
* <pre>
5+
* <a href="https://leetcode.com/problems/sqrtx/description/">Task description</a>
6+
*
7+
* Given a non-negative integer x, return the square root of x rounded down to the nearest integer.
8+
* The returned integer should be non-negative as well.
9+
* You must not use any built-in exponent function or operator.
10+
* For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
11+
*
12+
* Example 1:
13+
* Input: x = 4
14+
* Output: 2
15+
* Explanation: The square root of 4 is 2, so we return 2.
16+
*
17+
* Example 2:
18+
* Input: x = 8
19+
* Output: 2
20+
* Explanation: The square root of 8 is 2.82842...,
21+
* and since we round it down to the nearest integer, 2 is returned.
22+
* </pre>
23+
*
24+
* @see <a href="https://youtu.be/41zAzebmOuc">Video solution</a>
25+
*/
26+
public class SquareRootBabylon {
27+
28+
public static int mySqrt(int x) {
29+
double old = x;
30+
double root = x;
31+
do {
32+
old = root;
33+
root = 0.5 * (root + x / root);
34+
} while (Math.abs(root - old) >= 1);
35+
return (int) root;
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package by.andd3dfx.numeric;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class SquareRootBabylonTest {
8+
9+
@Test
10+
public void mySqrt() {
11+
assertThat(SquareRootBabylon.mySqrt(1)).isEqualTo(1);
12+
assertThat(SquareRootBabylon.mySqrt(2)).isEqualTo(1);
13+
assertThat(SquareRootBabylon.mySqrt(3)).isEqualTo(1);
14+
assertThat(SquareRootBabylon.mySqrt(4)).isEqualTo(2);
15+
assertThat(SquareRootBabylon.mySqrt(5)).isEqualTo(2);
16+
assertThat(SquareRootBabylon.mySqrt(8)).isEqualTo(2);
17+
assertThat(SquareRootBabylon.mySqrt(9)).isEqualTo(3);
18+
assertThat(SquareRootBabylon.mySqrt(624)).isEqualTo(24);
19+
assertThat(SquareRootBabylon.mySqrt(625)).isEqualTo(25);
20+
assertThat(SquareRootBabylon.mySqrt(789)).isEqualTo(28);
21+
}
22+
}

0 commit comments

Comments
 (0)