Python中是如何实现的内置函数POW()? [英] How did Python implement the built-in function pow()?
问题描述
我必须写一个程序来计算 A ** B%ç
,其中 B
和<$ C $ ç> C 都是非常大的数字。如果我只是用 A ** B%ç
,它真的很慢。后来我发现,内置函数 POW()
能做到这一点真的很快通过调用 POW(A,B,C)
。
我很好奇,想知道如何Python中实现这一点?或者我能找到实现该功能的源$ C $ C文件?
I have to write a program to calculate a**b % c
where b
and c
are both very large numbers. If I just use a**b % c
, it's really slow. Then I found that the built-in function pow()
can do this really fast by calling pow(a, b, c)
.
I'm curious to know how does Python implement this? Or where could I find the source code file that implement this function?
推荐答案
如果 A
, B
和ç
是整数,实施可以由二进制幂更有效并减少每一步模 C
,其中包括第一个(即减少 A
模 C
之前,你甚至开始)。这是 long_pow的实现()
确实如此。
If a
, b
and c
are integers, the implementation can be made more efficient by binary exponentiation and reducing modulo c
in each step, including the first one (i.e. reducing a
modulo c
before you even start). This is what the implementation of long_pow()
does indeed.
这篇关于Python中是如何实现的内置函数POW()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!