How to find the nth root of a value?
We know that the Nth root of a number, x, is equivalent of raising x to a power that is the reciprocal of N. Knowing this, we can use the pow
function to find the Nth root:
let nthRoot = pow(base, (1/n))
where base
and n
are floating point variables.
JavaScript: Calculate the nth root of a number
Can you use something like this?
Math.pow(n, 1/root);
eg.
Math.pow(25, 1/2) == 5
nth root of a number
You need to read "What Every Computer Scientist Should Know About Floating-Point Arithmetic".
Floating point numbers—which are what is normally used to represent non-integers—are inherently limited. Those limits allow good performance but at the cost of such anomalies.
nth roots in JavaScript
Math.pow(x, 1/y)
is y
-root of x
:
const pow_4 = Math.pow(2, 4) // 2^4
console.log(pow_4) // 16
const sqrt_4 = Math.pow(pow_4, 1/4) // sqrt root lvl 4 of 16
console.log(sqrt_4) // 2
Binary search to find nth root of a number
Your function breaks because the difference between (mid-DELTA)^n and mid^n is usually more than DELTA.
Binary search with doubles can be tricky, and there are various ways to do it depending on what result you really want. In this case, you are asked to give an answer within 0.001 of the actual root. It is not required that your answer^n is within 0.001 of x.
That suggests an implementation like this:
double binarysearch(double x, int n)
{
double lo = 0.0;
double hi = x;
while(hi-lo >= 0.0019)
{
double test = lo+(hi-lo)*0.5;
if (test == low || test == hi)
{
//I couldn't bear to leave this out. Sometimes there are no
//double values between lo and hi. This will be pretty much
//as close as we can get. Break here to avoid an infinite loop
break;
}
if (pow(test,n) < x)
{
lo = test;
}
else
{
hi = test;
}
}
//cut the final range in half. It's less than
//0.0019 in size, so every number in the range, which includes
//the root, is less than 0.001 away from this
return lo+(hi-lo)*0.5;
}
Notice that there is no possibility to return "not found"
Algorithm to find nth root of a number
#include <math.h>
inline int root(int input, int n)
{
return round(pow(input, 1./n));
}
This works for pretty much the whole integer range (as IEEE754 8-byte double
s can represent the whole 32-bit int
range exactly, which are the representations and sizes that are used on pretty much every system). And I doubt any integer based algorithm is faster on non-ancient hardware. Including ARM. Embedded controllers (the microwave washing machine kind) might not have floating point hardware though. But that part of the question was underspecified.
Is there a short-hand for nth root of x in Python?
nth root of x
is x^(1/n)
, so you can do 9**(1/2)
to find the 2nd root of 9, for example. In general, you can compute the nth root of x as:
x**(1/n)
Note: In Python 2, you had to do 1/float(n)
or 1.0/n
so that the result would be a float
rather than an int
. For more details, see Why does Python give the "wrong" answer for square root?
Related Topics
Programmatically Place Partial Image Over Another in UIview Using Swift 3
How to Use @Fetchrequest Outside a View
Uibutton Action Is Not Triggered After Constraint Layouts Changed
Swift Display Image UIimageview
Enum of String Type Vs Struct with Static Constant
Why Is The Leading Swipe Action Also Duplicated as a Trailing Action
What Does an Underscore "_" Mean in Swift
Swiftui: What Are The Differences Between Image and UIimage
Confusing About @Environmentobject in Swiftui
Swift 3 Google Map Add Markers on Touch
Custom Radix Columns (+Special Characters)
Swift - How to Deal with Uncaught Exception
Which Optimization Level Should I Choose for Release
Swift Combine How Set<Anycancellable> Works