实现基于整数的幂函数 pow(int, int) 的最有效方法
在 C 中,将一个整数求另一个整数次方的最有效方法是什么?
// 2^3
pow(2,3) == 8
// 5^5
pow(5,5) == 3125
What is the most efficient way given to raise an integer to the power of another integer in C?
// 2^3
pow(2,3) == 8
// 5^5
pow(5,5) == 3125
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
我的情况有点不同,我正在尝试从权力创建一个面具,但我想无论如何我都会分享我找到的解决方案。
显然,它只适用于 2 的幂。
My case is a little different, I'm trying to create a mask from a power, but I thought I'd share the solution I found anyway.
Obviously, it only works for powers of 2.
如果您在编译时知道指数(并且它是整数),则可以使用模板来展开循环。 这可以变得更高效,但我想在这里演示基本原理:
我们使用模板专业化终止递归:
需要在运行时知道指数,
In case you know the exponent (and it is an integer) at compile-time, you can use templates to unroll the loop. This can be made more efficient, but I wanted to demonstrate the basic principle here:
We terminate the recursion using a template specialization:
The exponent needs to be known at runtime,
正如对平方求幂效率的评论的后续。
该方法的优点是它在 log(n) 时间内运行。 例如,如果您要计算一些巨大的数据,例如 x^1048575 (2^20 - 1),则只需执行循环 20 次,而不是使用朴素方法执行 100 万次以上。
此外,就代码复杂性而言,它比按照普拉莫德的建议尝试找到最佳乘法序列更简单。
编辑:
我想我应该在有人标记我可能溢出之前澄清一下。 这种方法假设您有某种巨大的int库。
Just as a follow up to comments on the efficiency of exponentiation by squaring.
The advantage of that approach is that it runs in log(n) time. For example, if you were going to calculate something huge, such as x^1048575 (2^20 - 1), you only have to go thru the loop 20 times, not 1 million+ using the naive approach.
Also, in terms of code complexity, it is simpler than trying to find the most optimal sequence of multiplications, a la Pramod's suggestion.
Edit:
I guess I should clarify before someone tags me for the potential for overflow. This approach assumes that you have some sort of hugeint library.
迟到了:
下面是一个解决方案,也处理
y < 0 尽可能好。
intmax_t
的结果作为最大范围。 没有规定不适合intmax_t
的答案。pow(0,negative)
,另一个未定义的结果,返回INTMAX_MAX
此代码使用永远循环
for(;;)< /code> 以避免其他循环解决方案中常见的最终
base *= base
。 该乘法是 1) 不需要的,2) 可能是 int*int 溢出,即 UB。Late to the party:
Below is a solution that also deals with
y < 0
as best as it can.intmax_t
for maximum range. There is no provision for answers that do not fit inintmax_t
.powjii(0, 0) --> 1
which is a common result for this case.pow(0,negative)
, another undefined result, returnsINTMAX_MAX
This code uses a forever loop
for(;;)
to avoid the finalbase *= base
common in other looped solutions. That multiplication is 1) not needed and 2) could beint*int
overflow which is UB.是的,它是递归的,但是一个好的优化编译器会优化递归。
Yes, it's recursive, but a good optimizing compiler will optimize recursion away.
考虑负指数的更通用的解决方案
more generic solution considering negative exponenet
除了 Elias 的答案(使用有符号整数实现时会导致未定义的行为,以及使用无符号整数实现时高输入的错误值)之外,
这里是平方求幂的修改版本,它也适用于有符号整数类型,并且不不要给出不正确的值:
此函数的注意事项:
如果要发生任何溢出或换行,
return 0;
我使用了
int64_t
,但任何宽度(有符号或无符号) )只需稍作修改即可使用。 但是,如果您需要使用非固定宽度的整数类型,则需要将SQRT_INT64_MAX
更改为(int)sqrt(INT_MAX)
(在使用int
) 或类似的东西,应该优化,但它更难看,而且不是 C 常量表达式。 另外,将sqrt()
的结果转换为int
也不是很好,因为在完美平方的情况下,浮点精度很高,但我不知道有任何实现其中INT_MAX
- 或任何类型的最大值 - 是完全平方数,您可以接受。In addition to the answer by Elias, which causes Undefined Behaviour when implemented with signed integers, and incorrect values for high input when implemented with unsigned integers,
here is a modified version of the Exponentiation by Squaring that also works with signed integer types, and doesn't give incorrect values:
Considerations for this function:
If any overflow or wrapping is going to take place,
return 0;
I used
int64_t
, but any width (signed or unsigned) can be used with little modification. However, if you need to use a non-fixed-width integer type, you will need to changeSQRT_INT64_MAX
by(int)sqrt(INT_MAX)
(in the case of usingint
) or something similar, which should be optimized, but it is uglier, and not a C constant expression. Also casting the result ofsqrt()
to anint
is not very good because of floating point precission in case of a perfect square, but as I don't know of any implementation whereINT_MAX
-or the maximum of any type- is a perfect square, you can live with that.请注意,平方求幂并不是最佳方法。 作为适用于所有指数值的通用方法,这可能是您能做的最好的事情,但对于特定的指数值,可能有一个需要更少乘法的更好的序列。
例如,如果您想计算 x^15,则平方求幂的方法将为您提供:
这总共是 6 次乘法。
事实证明,这可以通过加法链求幂使用“仅”5次乘法来完成。
没有有效的算法来找到最佳的乘法序列。 来自维基百科:
Note that exponentiation by squaring is not the most optimal method. It is probably the best you can do as a general method that works for all exponent values, but for a specific exponent value there might be a better sequence that needs fewer multiplications.
For instance, if you want to compute x^15, the method of exponentiation by squaring will give you:
This is a total of 6 multiplications.
It turns out this can be done using "just" 5 multiplications via addition-chain exponentiation.
There are no efficient algorithms to find this optimal sequence of multiplications. From Wikipedia:
如果您需要计算 2 的幂。 最快的方法是按功率进行位移位。
If you need to raise 2 to a power. The fastest way to do so is to bit shift by the power.
这是Java中的方法
Here is the method in Java
power()
函数适用于仅限整数复杂度 = O(log(exp))
power()
函数适用于负exp和浮动基数。复杂度 = O(log(exp))
power()
function to work for Integers OnlyComplexity = O(log(exp))
power()
function to work for negative exp and float base.Complexity = O(log(exp))
一个极其特殊的情况是,当您需要说 2^(-x 到 y) 时,其中 x, 当然是负数,并且 y 太大而无法对 int 进行移位。 您仍然可以通过拧紧浮子在恒定时间内完成 2^x 。
通过使用 double 作为基本类型,您可以获得更多的 2 的幂。
(非常感谢评论者帮助解决这篇文章)。
还有一种可能性是,进一步了解 IEEE 浮点数,其他求幂的特殊情况可能会出现。
An extremely specialized case is, when you need say 2^(-x to the y), where x, is of course is negative and y is too large to do shifting on an int. You can still do 2^x in constant time by screwing with a float.
You can get more powers of 2 by using a double as the base type.
(Thanks a lot to commenters for helping to square this post away).
There's also the possibility that learning more about IEEE floats, other special cases of exponentiation might present themselves.
如果您想获取 2 的整数次幂,最好使用移位选项:
pow(2,5)
可以替换为1<; <5
这样效率更高。
If you want to get the value of an integer for 2 raised to the power of something it is always better to use the shift option:
pow(2,5)
can be replaced by1<<5
This is much more efficient.
通过平方求幂。
这是非对称密码学中对大量数字进行模幂运算的标准方法。
Exponentiation by squaring.
This is the standard method for doing modular exponentiation for huge numbers in asymmetric cryptography.
Swift 中的 O(log N) 解决方案...
The O(log N) solution in Swift...
另一种实现(用 Java 实现)。 可能不是最有效的解决方案,但迭代次数与指数解决方案相同。
One more implementation (in Java). May not be most efficient solution but # of iterations is same as that of Exponential solution.
我使用递归,如果exp是偶数,5^10 =25^5。
I use recursive, if the exp is even,5^10 =25^5.
我已经实现了记住所有计算功率的算法,然后在需要时使用它们。 例如,x^13 等于 (x^2)^2^2 * x^2^2 * x,其中 x^2^2 是从表中获取的,而不是再次计算。 这基本上是 @Pramod 答案的实现(但在 C# 中)。
需要的乘法次数是 Ceil(Log n)
I have implemented algorithm that memorizes all computed powers and then uses them when need. So for example x^13 is equal to (x^2)^2^2 * x^2^2 * x where x^2^2 it taken from the table instead of computing it once again. This is basically implementation of @Pramod answer (but in C#).
The number of multiplication needed is Ceil(Log n)
这是一个用于计算
x ** y
的 O(1) 算法,灵感来自于 此评论。 它适用于 32 位有符号int
。对于较小的
y
值,它使用平方求幂。 对于较大的y
值,只有少数x
值的结果不会溢出。 该实现使用查找表来读取结果而不进行计算。在溢出时,C 标准允许任何行为,包括崩溃。 然而,我决定对 LUT 索引进行边界检查,以防止内存访问冲突,这可能会令人惊讶且不受欢迎。
伪代码:
C 代码:
Here is a O(1) algorithm for calculating
x ** y
, inspired by this comment. It works for 32-bit signedint
.For small values of
y
, it uses exponentiation by squaring. For large values ofy
, there are only a few values ofx
where the result doesn't overflow. This implementation uses a lookup table to read the result without calculating.On overflow, the C standard permits any behavior, including crash. However, I decided to do bound-checking on LUT indices to prevent memory access violation, which could be surprising and undesirable.
Pseudo-code:
C code: