Skip to content

Commit 101d08a

Browse files
Add Square Root by Babylonian Method (TheAlgorithms#2883)
1 parent e4fa83b commit 101d08a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.Scanner;
4+
5+
6+
public class SquareRootWithBabylonianMethod {
7+
/**
8+
* get the value, return the square root
9+
*
10+
* @param num contains elements
11+
* @return the square root of num
12+
*/
13+
public static float square_Root(float num)
14+
{
15+
float a = num;
16+
float b = 1;
17+
double e = 0.000001;
18+
while (a - b > e) {
19+
a = (a + b) / 2;
20+
b = num / a;
21+
}
22+
return a;
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SquareRootwithBabylonianMethodTest {
7+
@Test
8+
void testfor4(){
9+
Assertions.assertEquals(2,SquareRootWithBabylonianMethod.square_Root(4));
10+
}
11+
12+
@Test
13+
void testfor1(){
14+
Assertions.assertEquals(1,SquareRootWithBabylonianMethod.square_Root(1));
15+
}
16+
17+
@Test
18+
void testfor2(){
19+
Assertions.assertEquals(1.4142135381698608,SquareRootWithBabylonianMethod.square_Root(2));
20+
}
21+
22+
@Test
23+
void testfor625(){
24+
Assertions.assertEquals(25,SquareRootWithBabylonianMethod.square_Root(625));
25+
}
26+
}

0 commit comments

Comments
 (0)