Bakshali逼近是一种找到数字平方根的逼近的数学方法。它等效于巴比伦方法的两次迭代。
算法:
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 + P
Step 5: Sqrt(S) will be nearly equal to A - (P2/2*A)
下面是上述步骤的实现。
执行:
C++
//This program gives result approximated to 5 decimal places.
#include
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(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 "<
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
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
输出 :
Square root of 9.2345 = 3.03883
插图:
find sqrt(9.2345)
S = 9.2345
N = 3
d = 9.2345 – (3^2) = 0.2345
P = 0.2345/(2*3) = 0.0391
A = 3 + 0.0391 = 3.0391
therefore, sqrt(9.2345) = 3.0391 – (0.0391^2/(2*0.0391)) = 3.0388
重要事项:
- 用于查找平方根的近似值。
- 需要需要计算其平方根的数字的最接近完美平方的值。
- 浮点数比整数更有效,因为它可以近似。