Bakshali近似是一种求一个数的平方根近似值的数学方法。它相当于 巴比伦法 . 算法:
null
To calculate sqrt(S).Step 1: Calculate nearest perfect square to S i.e (N2).Step 2: Calculate d = S - (N2)Step 3: Calculate P = d/(2*N)Step 4: Calculate A = N + PStep 5: Sqrt(S) will be nearly equal to A - (P2/2*A)
以下是上述步骤的实施情况。 实施:
C++
//This program gives result approximated to 5 decimal places. #include <iostream> float sqroot( float s) { int pSq = 0; //This will be the nearest perfect square to s int N = 0; //This is the sqrt of pSq // Find the nearest perfect square to s for ( int i = static_cast < int >(s); i > 0; i--) { for ( int j = 1; j < i; j++) { if (j*j == i) { pSq = i; N = j; break ; } } if (pSq > 0) break ; } float d = s - pSq; //calculate d float P = d/(2.0*N); //calculate P float A = N+P; //calculate A float sqrt_of_s = A-((P*P)/(2.0*A)); //calculate sqrt(S). return sqrt_of_s; } // Driver program to test above function int main() { float num = 9.2345; float sqroot_of_num = sqroot(num); std::cout << "Square root of " <<num<< " = " <<sqroot_of_num; return 0; } |
JAVA
// Java program gives result approximated // to 5 decimal places. class GFG { static float sqroot( float s) { // This will be the nearest perfect square to s int pSq = 0 ; //This is the sqrt of pSq int N = 0 ; // Find the nearest perfect square to s for ( int i = ( int )(s); i > 0 ; i--) { for ( int j = 1 ; j < i; j++) { if (j*j == i) { pSq = i; N = j; break ; } } if (pSq > 0 ) break ; } // calculate d float d = s - pSq; // calculate P float P = d/( 2 .0f*N); // calculate A float A = N+P; // calculate sqrt(S). float sqrt_of_s = A-((P*P)/( 2 .0f*A)); return sqrt_of_s; } // Driver program public static void main (String[] args) { float num = 9 .2345f; float sqroot_of_num = sqroot(num); System.out.print( "Square root of " +num+ " = " + Math.round(sqroot_of_num* 100000.0 )/ 100000.0 ); } } // This code is contributed by Anant Agarwal. |
Python3
# This Python3 program gives result # approximated to 5 decimal places. def sqroot(s): # This will be the nearest # perfect square to s pSq = 0 ; # This is the sqrt of pSq N = 0 ; # Find the nearest # perfect square to s for i in range ( int (s), 0 , - 1 ): for j in range ( 1 , i): if (j * j = = i): pSq = i; N = j; break ; if (pSq > 0 ): break ; d = s - pSq; # calculate d P = d / ( 2.0 * N); # calculate P A = N + P; # calculate A # calculate sqrt(S). sqrt_of_s = A - ((P * P) / ( 2.0 * A)); return sqrt_of_s; # Driver Code num = 9.2345 ; sqroot_of_num = sqroot(num); print ( "Square root of " , num, "=" , round ((sqroot_of_num * 100000.0 ) / 100000.0 , 5 )); # This code is contributed by mits |
C#
// C# program gives result approximated // to 5 decimal places. using System; class GFG { static float sqroot( float s) { // This will be the nearest // perfect square to s int pSq = 0; //This is the sqrt of pSq int N = 0; // Find the nearest perfect square to s for ( int i = ( int )(s); i > 0; i--) { for ( int j = 1; j < i; j++) { if (j * j == i) { pSq = i; N = j; break ; } } if (pSq > 0) break ; } // calculate d float d = s - pSq; // calculate P float P = d / (2.0f * N); // calculate A float A = N + P; // calculate sqrt(S). float sqrt_of_s = A-((P * P) / (2.0f * A)); return sqrt_of_s; } // Driver Code public static void Main () { float num = 9.2345f; float sqroot_of_num = sqroot(num); Console.Write( "Square root of " +num+ " = " + Math.Round(sqroot_of_num * 100000.0) / 100000.0); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // This PHP program gives result // approximated to 5 decimal places. function sqroot( $s ) { // This will be the nearest // perfect square to s $pSq = 0; //This is the sqrt of pSq $N = 0; // Find the nearest // perfect square to s for ( $i = intval ( $s ); $i > 0; $i --) { for ( $j = 1; $j < $i ; $j ++) { if ( $j * $j == $i ) { $pSq = $i ; $N = $j ; break ; } } if ( $pSq > 0) break ; } $d = $s - $pSq ; //calculate d $P = $d / (2.0 * $N ); //calculate P $A = $N + $P ; //calculate A //calculate sqrt(S). $sqrt_of_s = $A - (( $P * $P ) / (2.0 * $A )); return $sqrt_of_s ; } // Driver Code $num = 9.2345; $sqroot_of_num = sqroot( $num ); echo "Square root of " . $num . " = " . round (( $sqroot_of_num * 100000.0) / 100000.0, 5); // This code is contributed by Sam007 ?> |
Javascript
<script> // javascript program gives result approximated // to 5 decimal places.{ function sqroot(s) { // This will be the nearest perfect square to s var pSq = 0; // This is the sqrt of pSq var N = 0; // Find the nearest perfect square to s for (i = parseInt(s); i > 0; i--) { for (j = 1; j < i; j++) { if (j*j == i) { pSq = i; N = j; break ; } } if (pSq > 0) break ; } // calculate d var d = s - pSq; // calculate P var P = (d/(2.0*N)); // calculate A var A = N+P; // calculate sqrt(S). var sqrt_of_s = A-((P*P)/(2.0*A)); return sqrt_of_s; } // Driver program var num = 9.2345; var sqroot_of_num = sqroot(num); document.write( "Square root of " +num+ " = " + (Math.round(sqroot_of_num*100000.0)/100000.0).toFixed(6)); // This code is contributed by Princi Singh </script> |
输出:
Square root of 9.2345 = 3.03883
插图:
find sqrt(9.2345)S = 9.2345N = 3 d = 9.2345 – (3^2) = 0.2345P = 0.2345/(2*3) = 0.0391A = 3 + 0.0391 = 3.0391therefore, sqrt(9.2345) = 3.0391 – (0.0391^2/(2*0.0391)) = 3.0388
要点:
- 曾经发现 近似值 到平方根。
- 需要计算其平方根的数字的最近完美平方的值。
- 在找到近似值时,浮点数比整数更有效。
参考: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Bakhshali_approximation 本文由 严酷的阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END