Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit101d08a

Browse files
Add Square Root by Babylonian Method (TheAlgorithms#2883)
1 parente4fa83b commit101d08a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packagecom.thealgorithms.maths;
2+
3+
importjava.util.Scanner;
4+
5+
6+
publicclassSquareRootWithBabylonianMethod {
7+
/**
8+
* get the value, return the square root
9+
*
10+
* @param num contains elements
11+
* @return the square root of num
12+
*/
13+
publicstaticfloatsquare_Root(floatnum)
14+
{
15+
floata =num;
16+
floatb =1;
17+
doublee =0.000001;
18+
while (a -b >e) {
19+
a = (a +b) /2;
20+
b =num /a;
21+
}
22+
returna;
23+
}
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.thealgorithms.maths;
2+
3+
importorg.junit.jupiter.api.Assertions;
4+
importorg.junit.jupiter.api.Test;
5+
6+
publicclassSquareRootwithBabylonianMethodTest {
7+
@Test
8+
voidtestfor4(){
9+
Assertions.assertEquals(2,SquareRootWithBabylonianMethod.square_Root(4));
10+
}
11+
12+
@Test
13+
voidtestfor1(){
14+
Assertions.assertEquals(1,SquareRootWithBabylonianMethod.square_Root(1));
15+
}
16+
17+
@Test
18+
voidtestfor2(){
19+
Assertions.assertEquals(1.4142135381698608,SquareRootWithBabylonianMethod.square_Root(2));
20+
}
21+
22+
@Test
23+
voidtestfor625(){
24+
Assertions.assertEquals(25,SquareRootWithBabylonianMethod.square_Root(625));
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp