logo

sqrt メソッドを使用せずに数値の平方根を求める Java プログラム

Javaでは、 数値の平方根を求める 事前定義されたメソッドを使用する場合は非常に簡単です。ジャワ 数学 クラスが提供する sqrt() 数値の平方根を求めるメソッド。このセクションでは、 sqrt() メソッドを使用せずに数値の平方根を求める Java プログラム 。一番人気です 質問 で尋ねた Java インタビュー

数値の 2 乗が x の場合、その数値の平方根は数値そのものを掛けたものになります。たとえば、625 の平方根は 25 です。25 を 2 回乗算すると、数値の 2 乗が得られます。数学的には、数値の平方根は次のように与えられます。

x=√バツ

数値の平方根を求めるには次の公式を使用しました。

平方メートルn+1=(sqrtn+(数値/平方n))/2.0

注: 最初の平方数は入力数値/2 である必要があります。

上記の式を実装してみましょう Javaプログラム そして平方根を求めます。

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:

 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 

平方根を求める別のロジックを見てみましょう。

次の例では、次の手順を使用して平方根を求めています。

  • イテレータ変数を初期化しました i=1
  • 入力した数値が完全な平方であるかどうかを確認してください。もし i の 2 乗は n に等しい、i n の平方根になります。
  • それ以外の場合は、 i の最小値 。の二乗を思い出してください。 私はnより大きくなければなりません 。数値の平方根は次の範囲にあります i-1 そして 。手順を実行した後、 二分探索アルゴリズム 小数点以下 n 桁までの数値の平方根を求めます。
  • 変数 i をインクリメントします 1

二分探索アルゴリズム

  • を見つける 中間値 i-1とiの。
  • の平方根を求めます 中間値 そしてそれをnと比較します。
    • もし 中間値 * 中間値 = n 、中間値は指定された数値の平方根です。中間値の 2 乗を 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