-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution of SquareRoot task using Babylon method
- Loading branch information
1 parent
977d6f2
commit 6816b22
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/test/java/by/andd3dfx/numeric/SquareRootBabylonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |