整数n次方根方根、整数

2023-09-11 23:04:02 作者:梦在深巷.

X'是y的n次方根如果x'是最大的整数,使得x ^ N&其中; = Y。 X,X'和y都是整数。有没有计算这样的n次方根的任何有效的方法?我知道这通常是由 n次方根算法,但这里的困难是一切都是整数,因为我敢与嵌入式系统的工作。

顺便说一句,我甚至试过二进制搜索从1到y,以确定最大的x,使得使得x ^ N< = Y,但它不工作,因为x的n次方溢出容易尤其是当n很大 解决方案

存储表的最大x使得x ^ Y不溢出给出年。使用这些值的二进制搜索;这样一来,没有更多的溢出和整齐的算法只要将被用作x和n具有相同的(整数)型。对吧?

注:Y> 32,x的最大值为2的32位整数...换句话说,表格将是相同的尺寸如在整数系统了解到,大约比特数

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.

解决方案 n次方根与根式

Store a table for given y of the maximum x such that x^y does not overflow. Use these values for binary search; that way, no more overflow and a neat algorithm that will work as long as x and n have the same (integer) type. Right?

Note: for y > 32, the maximum value for x is 2 for 32-bit integers... in other words, your table will be the same size as the number of bits in integers your system understands, approximately.