给定一个数字N ,任务是不使用sqrt()函数来查找N的平方根。
例子:
Input: N = 25
Output: 5
Input: N = 3
Output: 1.73205
Input: N = 2.5
Output: 1.58114
方法:
- 从i = 1开始迭代。如果i * i = n ,则将i打印为n是一个平方根为i的理想平方。
- 否则,找到i * i严格大于n的最小i 。
- 现在我们知道n的平方根位于i – 1和i的区间内,我们可以使用二进制搜索算法找到平方根。
- 找出i – 1和i的中点,然后将mid * mid与n进行比较,精度最高到5位小数。
- 如果mid * mid = n,则返回mid 。
- 如果mid * mid
则在下半部分重复出现。 - 如果mid * mid> n,则重复上半部分。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Recursive function that returns square root
// of a number with precision upto 5 decimal places
double Square(double n, double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;
// If mid itself is the square root,
// return mid
if ((mul == n) || (abs(mul - n) < 0.00001))
return mid;
// If mul is less than n, recur second half
else if (mul < n)
return Square(n, mid, j);
// Else recur first half
else
return Square(n, i, mid);
}
// Function to find the square root of n
void findSqrt(double n)
{
double i = 1;
// While the square root is not found
bool found = false;
while (!found) {
// If n is a perfect square
if (i * i == n) {
cout << fixed << setprecision(0) << i;
found = true;
}
else if (i * i > n) {
// Square root will lie in the
// interval i-1 and i
double res = Square(n, i - 1, i);
cout << fixed << setprecision(5) << res;
found = true;
}
i++;
}
}
// Driver code
int main()
{
double n = 3;
findSqrt(n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Recursive function that returns
// square root of a number with
// precision upto 5 decimal places
static double Square(double n,
double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;
// If mid itself is the square root,
// return mid
if ((mul == n) ||
(Math.abs(mul - n) < 0.00001))
return mid;
// If mul is less than n,
// recur second half
else if (mul < n)
return Square(n, mid, j);
// Else recur first half
else
return Square(n, i, mid);
}
// Function to find the square root of n
static void findSqrt(double n)
{
double i = 1;
// While the square root is not found
boolean found = false;
while (!found)
{
// If n is a perfect square
if (i * i == n)
{
System.out.println(i);
found = true;
}
else if (i * i > n)
{
// Square root will lie in the
// interval i-1 and i
double res = Square(n, i - 1, i);
System.out.printf("%.5f", res);
found = true;
}
i++;
}
}
// Driver code
public static void main(String[] args)
{
double n = 3;
findSqrt(n);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
import math
# Recursive function that returns square root
# of a number with precision upto 5 decimal places
def Square(n, i, j):
mid = (i + j) / 2;
mul = mid * mid;
# If mid itself is the square root,
# return mid
if ((mul == n) or (abs(mul - n) < 0.00001)):
return mid;
# If mul is less than n, recur second half
elif (mul < n):
return Square(n, mid, j);
# Else recur first half
else:
return Square(n, i, mid);
# Function to find the square root of n
def findSqrt(n):
i = 1;
# While the square root is not found
found = False;
while (found == False):
# If n is a perfect square
if (i * i == n):
print(i);
found = True;
elif (i * i > n):
# Square root will lie in the
# interval i-1 and i
res = Square(n, i - 1, i);
print ("{0:.5f}".format(res))
found = True
i += 1;
# Driver code
if __name__ == '__main__':
n = 3;
findSqrt(n);
# This code is contributed by 29AjayKumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Recursive function that returns
// square root of a number with
// precision upto 5 decimal places
static double Square(double n,
double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;
// If mid itself is the square root,
// return mid
if ((mul == n) ||
(Math.Abs(mul - n) < 0.00001))
return mid;
// If mul is less than n,
// recur second half
else if (mul < n)
return Square(n, mid, j);
// Else recur first half
else
return Square(n, i, mid);
}
// Function to find the square root of n
static void findSqrt(double n)
{
double i = 1;
// While the square root is not found
Boolean found = false;
while (!found)
{
// If n is a perfect square
if (i * i == n)
{
Console.WriteLine(i);
found = true;
}
else if (i * i > n)
{
// Square root will lie in the
// interval i-1 and i
double res = Square(n, i - 1, i);
Console.Write("{0:F5}", res);
found = true;
}
i++;
}
}
// Driver code
public static void Main(String[] args)
{
double n = 3;
findSqrt(n);
}
}
// This code is contributed by Princi Singh
输出:
1.73205