[Algorithm] Java 제곱근 - 바빌로니아

Gogh·2023년 1월 2일
0

Algorithm

목록 보기
10/10

🎯 목표 :  바빌로니아 방법으로 제곱근 구하기

📒 Babylonian Method

  • 다양한 방법으로 제곱근을 구할수 있지만, 내장 클래스를 사용하지 않고 제곱근을 가장 간단하게 구하는 바빌로니아 법이 있다.
public class Solution {
    private static final int ACCUR = 10;

	public String squareRoot(int num) {
    double sqrt = PRECISION;

    for (int i = 0; i < ACCUR; i++)
        sqrt = 0.5 * (num / sqrt + sqrt);
	// 소숫점 둘째 자리까지만 출력
    return String.format("%.2f", sqrt);
	}
}

References

https://ko.wikipedia.org/wiki/%EB%B0%94%EB%B9%8C%EB%A1%9C%EB%8B%88%EC%95%84_%EB%B2%95
https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method

profile
컴퓨터가 할일은 컴퓨터가

0개의 댓글