nth-root

How to calculate Python float-number-th root of float number

ぐ巨炮叔叔 提交于 2019-12-25 17:05:42
问题 I found the following answer here on Stackoverflow: https://stackoverflow.com/a/356187/1829329 But it only works for integers as n in nth root: import gmpy2 as gmpy result = gmpy.root((1/0.213), 31.5).real print('result:', result) results in: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-eb4628226deb> in <module>() 8 ----> 9 result = gmpy.root((1/0.213), 31.5).real 10 11 print('result:', result)

Calculating the n-th root of an integer using PHP/GMP

 ̄綄美尐妖づ 提交于 2019-12-08 00:07:00
问题 How can I calculate the n-th root of an integer using PHP/GMP? Although I found a function called gmp_root(a, nth) in the PHP source, it seems that this function has not been published in any release yet*: http://3v4l.org/8FjU7 *) 5.6.0alpha2 being the most recent one at the time of writing 回答1: Original source: Calculating Nth root with bcmath in PHP – thanks and credits to HamZa! I've rewritten the code to use GMP instead of BCMath: function gmp_nth_root($num, $n) { if ($n < 1) return 0; //

n-th Root Algorithm

十年热恋 提交于 2019-12-07 08:20:18
问题 What is the fastest way to calculate the n -th root of a number? I'm aware of the Try and Fail method, but I need a faster algorithm. 回答1: The canonical way to do this is Newton's Method. In case you don't know, the derivative of x n is nx n-1 . This will come in handy. 1 is a good first guess. You want to apply it to the function a - x n IIRC, it's superconvergent on functions of the form a - x n , but either way, it's quite fast. Also, IIRC, the warning in the wiki about it failing to

Calculating the n-th root of an integer using PHP/GMP

假如想象 提交于 2019-12-06 12:04:46
How can I calculate the n-th root of an integer using PHP/GMP? Although I found a function called gmp_root(a, nth) in the PHP source , it seems that this function has not been published in any release yet*: http://3v4l.org/8FjU7 *) 5.6.0alpha2 being the most recent one at the time of writing ComFreek Original source: Calculating Nth root with bcmath in PHP – thanks and credits to HamZa ! I've rewritten the code to use GMP instead of BCMath: function gmp_nth_root($num, $n) { if ($n < 1) return 0; // we want positive exponents if ($num <= 0) return 0; // we want positive numbers if ($num < 2)

Wrong answer while calculating the nth root of number in cpp

╄→尐↘猪︶ㄣ 提交于 2019-12-02 19:54:18
问题 I am calculating the n'th root of a positive integer using standard library method pow() .Here is the snippet from my program : double x,y; x=pow(64,(1.0/3)); int z; printf("x=%lf\n",x); z=(int)x; printf("%d\n",z); But while finding cube root of 64. X is printed as 4.000000 while z as 3. Why so ? Can someone suggest a better algorithm , for the same ? 回答1: If you print more digits on x , you'll see what the problem is (I chose 30 randomly): double x ; x = pow(64, 1.0/3); printf("x=%.30lf\n",x

Wrong answer while calculating the nth root of number in cpp

扶醉桌前 提交于 2019-12-02 12:17:16
I am calculating the n'th root of a positive integer using standard library method pow() .Here is the snippet from my program : double x,y; x=pow(64,(1.0/3)); int z; printf("x=%lf\n",x); z=(int)x; printf("%d\n",z); But while finding cube root of 64. X is printed as 4.000000 while z as 3. Why so ? Can someone suggest a better algorithm , for the same ? If you print more digits on x , you'll see what the problem is (I chose 30 randomly): double x ; x = pow(64, 1.0/3); printf("x=%.30lf\n",x); Output: x=3.99999999...999600000000 So obvisouly, if you cast x into a int it will became 3 . There is

integer nth root

断了今生、忘了曾经 提交于 2019-12-01 02:44:09
问题 x' is the nth root of y if x' is the largest integer such that x^n <= y. x, x' and y are all integers. Is there any efficient way to compute such nth root? I know this is usually done by nth root algorithm, but the difficulty here is everything is integer because I'm working with an embedded system. BTW, I've even tried to binary search from 1 to y to identify largest x such that x^n <= y, but it does not work since x^n overflows easily especially when n is large. 回答1: Store a table for given

nth root implementation

社会主义新天地 提交于 2019-11-30 08:24:39
I am working on a way to calculate the n th root of a number. However, I am having problems with the n th root of negative numbers. Most people say to use Math.pow(num, 1 / root) , but this does not work for negative numbers. I have tried this: public static double root(double num, double root) { if (num < 0) { return -Math.pow(Math.abs(num), (1 / root)); } return Math.pow(num, 1.0 / root); } but, it does not work for all numbers as the root can be a decimal. For example root(-26, 0.8) returns -58.71 , but that is an invalid input. This will also give the wrong answer for even roots. For

nth root implementation

試著忘記壹切 提交于 2019-11-29 11:09:40
问题 I am working on a way to calculate the n th root of a number. However, I am having problems with the n th root of negative numbers. Most people say to use Math.pow(num, 1 / root) , but this does not work for negative numbers. I have tried this: public static double root(double num, double root) { if (num < 0) { return -Math.pow(Math.abs(num), (1 / root)); } return Math.pow(num, 1.0 / root); } but, it does not work for all numbers as the root can be a decimal. For example root(-26, 0.8)

How to compute the nth root of a very big integer

Deadly 提交于 2019-11-27 07:26:34
I need a way to compute the nth root of a long integer in Python. I tried pow(m, 1.0/n) , but it doesn't work: OverflowError: long int too large to convert to float Any ideas? By long integer I mean REALLY long integers like: 11968003966030964356885611480383408833172346450467339251 196093144141045683463085291115677488411620264826942334897996389 485046262847265769280883237649461122479734279424416861834396522 819159219215308460065265520143082728303864638821979329804885526 557893649662037092457130509980883789368448042961108430809620626