Skip to content

Commit

Permalink
Add solution of SquareRoot task using Babylon method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Nov 16, 2024
1 parent 977d6f2 commit 6816b22
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ they contain enough code which describes implementation in a natural way.
| Java интервью 11 | [Youtube](https://youtu.be/zufbVgdBCAI) | - |
| Java интервью 12 | [Youtube](https://youtu.be/PD41epg_pT4) | - |
| Функциональные тесты REST API с помощью Spock | [Youtube](https://youtu.be/GK5y3oA3qfM) | - |
| Вычисление квадратного корня вавилонским методом (leetcode) | [Youtube](https://youtu.be/41zAzebmOuc) | [Code](src/main/java/by/andd3dfx/numeric/SquareRootBabylon.java) |

## Materials & notes

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/by/andd3dfx/numeric/SquareRootBabylon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package by.andd3dfx.numeric;

/**
* <pre>
* <a href="https://leetcode.com/problems/sqrtx/description/">Task description</a>
*
* Given a non-negative integer x, return the square root of x rounded down to the nearest integer.
* The returned integer should be non-negative as well.
* You must not use any built-in exponent function or operator.
* For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
*
* Example 1:
* Input: x = 4
* Output: 2
* Explanation: The square root of 4 is 2, so we return 2.
*
* Example 2:
* Input: x = 8
* Output: 2
* Explanation: The square root of 8 is 2.82842...,
* and since we round it down to the nearest integer, 2 is returned.
* </pre>
*
* @see <a href="https://youtu.be/41zAzebmOuc">Video solution</a>
*/
public class SquareRootBabylon {

public static int mySqrt(int x) {
double old = x;
double root = x;
do {
old = root;
root = 0.5 * (root + x / root);
} while (Math.abs(root - old) >= 1);
return (int) root;
}
}
22 changes: 22 additions & 0 deletions src/test/java/by/andd3dfx/numeric/SquareRootBabylonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package by.andd3dfx.numeric;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class SquareRootBabylonTest {

@Test
public void mySqrt() {
assertThat(SquareRootBabylon.mySqrt(1)).isEqualTo(1);
assertThat(SquareRootBabylon.mySqrt(2)).isEqualTo(1);
assertThat(SquareRootBabylon.mySqrt(3)).isEqualTo(1);
assertThat(SquareRootBabylon.mySqrt(4)).isEqualTo(2);
assertThat(SquareRootBabylon.mySqrt(5)).isEqualTo(2);
assertThat(SquareRootBabylon.mySqrt(8)).isEqualTo(2);
assertThat(SquareRootBabylon.mySqrt(9)).isEqualTo(3);
assertThat(SquareRootBabylon.mySqrt(624)).isEqualTo(24);
assertThat(SquareRootBabylon.mySqrt(625)).isEqualTo(25);
assertThat(SquareRootBabylon.mySqrt(789)).isEqualTo(28);
}
}

0 comments on commit 6816b22

Please sign in to comment.