logo

sqrt 메소드 없이 숫자의 제곱근을 찾는 Java 프로그램

자바에서는 숫자의 제곱근을 구하다 미리 정의된 방법을 사용하면 매우 쉽습니다. 자바 수학 수업은 제공합니다 sqrt() 숫자의 제곱근을 구하는 방법. 이 섹션에서는 sqrt() 메소드를 사용하지 않고 숫자의 제곱근을 찾는 Java 프로그램 . 가장 인기가 많습니다 질문 에서 물었다 자바 인터뷰 .

숫자의 제곱이 x인 경우 해당 숫자의 제곱근은 숫자 자체를 곱한 값이 됩니다. 예를 들어 625의 제곱근은 25입니다. 25를 두 번 곱하면 숫자의 제곱이 됩니다. 수학적으로 숫자의 제곱근은 다음과 같이 제공됩니다.

x=√엑스

우리는 숫자의 제곱근을 찾기 위해 다음 공식을 사용했습니다.

sqrtn+1=(제곱N+(수/제곱N))/2.0

참고: 첫 번째 sqrt 숫자는 입력 숫자/2여야 합니다.

위의 공식을 다음과 같이 구현해보자. 자바 프로그램 그리고 제곱근을 구해 보세요.

FindSquareRootExample1 .java

 import java.util.Scanner; public class FindSquareRootExample1 { public static void main(String[] args) { System.out.print('Enter a number: '); //creating object of the Scanner class Scanner sc = new Scanner(System.in); //reading a number form the user int n = sc.nextInt(); //calling the method and prints the result System.out.println('The square root of '+ n+ ' is: '+squareRoot(n)); } //user-defined method that contains the logic to find the square root public static double squareRoot(int num) { //temporary variable double t; double sqrtroot=num/2; do { t=sqrtroot; sqrtroot=(t+(num/t))/2; } while((t-sqrtroot)!= 0); return sqrtroot; } } 

출력 1:

arraylist 메소드
 Enter a number: 12 The square root of 12 is: 3.4641016151377544 

출력 2:

 Enter a number: 25 The square root of 25 is: 5.0 

제곱근을 찾는 또 다른 논리를 살펴보겠습니다.

다음 예에서는 다음 절차를 사용하여 제곱근을 찾았습니다.

  • 반복자 변수를 초기화했습니다. 나는=1 .
  • 입력한 숫자가 완전제곱수인지 확인하세요. 만약 i의 제곱은 n과 같습니다. n의 제곱근 값이 됩니다.
  • 그렇지 않으면 다음을 찾으십시오. i의 가장 낮은 값 . 의 제곱이라는 것을 기억하세요 나는 n보다 커야 해 . 숫자의 제곱근은 다음 사이에 있습니다. i-1 그리고 . 단계를 수행한 후 다음을 사용합니다. 이진 검색 알고리즘 소수점 n자리까지의 숫자의 제곱근을 구합니다.
  • 변수 i를 다음만큼 증가시킵니다. 1 .

이진 검색 알고리즘

  • 찾기 중간값 i-1과 나.
  • 의 제곱을 구하세요 중간값 그리고 그것을 n과 비교해 보세요.
    • 만약에 중간값 * 중간값 = n , 중간값은 주어진 숫자의 제곱근입니다. 중간값의 제곱을 n(소수점 n자리까지)과 비교하여 차이가 작으면 중간값은 숫자의 제곱근이 됩니다.
    • 만약에 중간값 * 중간값 > n , 제곱근은 다음에 속합니다. 전반 .
    • 만약에 중간값 * 중간값, 제곱근은 다음에 속합니다. 후반 .

Java 프로그램에서 알고리즘을 구현하고 숫자의 제곱근을 찾아보겠습니다.

FindSquareRootExample2.java

 import java.util.Scanner; public class FindSquareRootExample2 { public static void main(String[] args) { double number = 0, sqrt=0; //object of the Scanner class Scanner sc = new Scanner(System.in); System.out.print('Enter a number: '); //reading a double value from the user number = sc.nextDouble(); //method calling sqrt = squareRoot(number); //prints the result System.out.println(The square root of '+number+ ' is ' +sqrt); } //user-defined method to find the square root of a number private static double squareRoot(double number) { //iterator variable int i = 1; while(true) { //for perfect square numbers if(i*i == number) return i; //for not perfect square numbers else if(i*i > number) //returns the value calculated by the method decimalSqrt() return decimalSqrt(number,i-1,i); //increments the variable i by 1 i++; } } // recursive method to find the square root of a number up to 7 decimal places private static double decimalSqrt(double number, double i, double j) //calculates the middle of i and j double midvalue = (i+j)/2; //finds the square of the midvalue double square = midvalue * midvalue; //compares the midvalue with square up to n decimal places if(square==number } 

출력 1:

라텍스 매트릭스
 Enter a number: 625 The square root of 625.0 is 25.0 

출력 2:

 Enter a number: 129 The square root of 129.0 is 11.357816688716412