Skip to content

Commit 07a4402

Browse files
Added Babylonian Square Root Method
1 parent 52a43fd commit 07a4402

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class BabylonianSquareRoot {
2+
3+
private static float babylonianSquareRoot(float num) throws ArithmeticException {
4+
5+
if (num < 0)
6+
throw new ArithmeticException("NaN");
7+
float x = num;
8+
float y = 1;
9+
float e = 0.00000001f;
10+
11+
while (x - y > e) {
12+
x = (x + y) / 2;
13+
y = num / x;
14+
}
15+
return x;
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(babylonianSquareRoot(52));
20+
}
21+
}

0 commit comments

Comments
 (0)