使用二分法查找数字的第 N 次根
给定两个正整数N和P 。任务是找到P的第N个根。
例子:
Input: P = 1234321, N = 2
Output: 1111
Explanation: square root of 1234321 is 1111.
Input: P = 123456785, N = 20
Output: 2.53849
方法:有多种方法可以解决给定的问题。这里下面的算法是基于数学概念称为二分法求根。为了找到给定数 P 的N次方根,我们将在x中形成一个方程,如 ( x p – P = 0 ),目标是使用二分法找到该方程的正根。
二分法如何工作?
取一个区间(a, b) ,使其已知根存在于该区间中。在此之后找到区间的中点并检查 函数的值,它在x = mid 处的导数。
- 如果函数的值为 0 表示找到了 root
- 如果函数的值为正且其导数为负,则表示根位于右半部分。
- 如果函数的值为正且其导数为正,则表示根位于左半部分。
下面是上述方法的实现:
C++14
// C++ program for above approach
#include
using namespace std;
// Function that returns the value
// of the function at a given value of x
double f(double x, int p, double num)
{
return pow(x, p) - num;
}
// calculating the value
// of the differential of the function
double f_prime(double x, int p)
{
return p * pow(x, p - 1);
}
// The function that returns
// the root of given number
double root(double num, int p)
{
// Defining range
// on which answer can be found
double left = -num;
double right = num;
double x;
while (true) {
// finding mid value
x = (left + right) / 2.0;
double value = f(x, p, num);
double prime = f_prime(x, p);
if (value * prime <= 0)
left = x;
else
right = x;
if (value < 0.000001 && value >= 0) {
return x;
}
}
}
// Driver code
int main()
{
double P = 1234321;
int N = 2;
double ans = root(P, N);
cout << ans;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function that returns the value
// of the function at a given value of x
static double f(double x, int p, double num)
{
return Math.pow(x, p) - num;
}
// calculating the value
// of the differential of the function
static double f_prime(double x, int p)
{
return p * Math.pow(x, p - 1);
}
// The function that returns
// the root of given number
static double root(double num, int p)
{
// Defining range
// on which answer can be found
double left = -num;
double right = num;
double x;
while (true) {
// finding mid value
x = (left + right) / 2.0;
double value = f(x, p, num);
double prime = f_prime(x, p);
if (value * prime <= 0)
left = x;
else
right = x;
if (value < 0.000001 && value >= 0) {
return x;
}
}
}
// Driver code
public static void main(String args[])
{
double P = 1234321;
int N = 2;
double ans = root(P, N);
System.out.print(ans);
}
}
// This code is contributed by ihritik
Python3
# python program for above approach
# Function that returns the value
# of the function at a given value of x
def f(x, p, num):
return pow(x, p) - num
# calculating the value
# of the differential of the function
def f_prime(x, p):
return p * pow(x, p - 1)
# The function that returns
# the root of given number
def root(num, p):
# Defining range
# on which answer can be found
left = -num
right = num
x = 0
while (True):
# finding mid value
x = (left + right) / 2.0
value = f(x, p, num)
prime = f_prime(x, p)
if (value * prime <= 0):
left = x
else:
right = x
if (value < 0.000001 and value >= 0):
return x
# Driver code
if __name__ == "__main__":
P = 1234321
N = 2
ans = root(P, N)
print(ans)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG
{
// Function that returns the value
// of the function at a given value of x
static double f(double x, int p, double num)
{
return Math.Pow(x, p) - num;
}
// calculating the value
// of the differential of the function
static double f_prime(double x, int p)
{
return p * Math.Pow(x, p - 1);
}
// The function that returns
// the root of given number
static double root(double num, int p)
{
// Defining range
// on which answer can be found
double left = -num;
double right = num;
double x;
while (true) {
// finding mid value
x = (left + right) / 2.0;
double value = f(x, p, num);
double prime = f_prime(x, p);
if (value * prime <= 0)
left = x;
else
right = x;
if (value < 0.000001 && value >= 0) {
return x;
}
}
}
// Driver code
public static void Main(string []args)
{
double P = 1234321;
int N = 2;
double ans = root(P, N);
Console.Write(ans);
}
}
// This code is contributed by AnkThon
Javascript
输出
1111
时间复杂度: O(log P)。
辅助空间: O(1)。