Skip to content

added java code of babylonian method #2883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.thealgorithms.maths;

import java.util.Scanner;


public class SquareRootWithBabylonianMethod {
/**
* get the value, return the square root
*
* @param num contains elements
* @return the square root of num
*/
public static float square_Root(float num)
{
float a = num;
float b = 1;
double e = 0.000001;
while (a - b > e) {
a = (a + b) / 2;
b = num / a;
}
return a;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thealgorithms.maths;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SquareRootwithBabylonianMethodTest {
@Test
void testfor4(){
Assertions.assertEquals(2,SquareRootWithBabylonianMethod.square_Root(4));
}

@Test
void testfor1(){
Assertions.assertEquals(1,SquareRootWithBabylonianMethod.square_Root(1));
}

@Test
void testfor2(){
Assertions.assertEquals(1.4142135381698608,SquareRootWithBabylonianMethod.square_Root(2));
}

@Test
void testfor625(){
Assertions.assertEquals(25,SquareRootWithBabylonianMethod.square_Root(625));
}
}