Python中是如何实现的内置函数POW()?如何实现、函数、Python、POW

由网友(怼烎)分享简介:我必须写一个程序来计算 A ** B%ç,其中 B 和 C 都是非常大的数字。如果我只是用 A ** B%ç,它真的很慢。后来我发现,内置函数 POW()能做到这一点真的很快通过调用 POW(A,B,C)。我很好奇,想知道如何Python中实现这一点?或者我能找到实现该功能的源$ C ​​$ C文件?I have t...

我必须写一个程序来计算 A ** B%ç,其中 B 和 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.

阅读全文

相关推荐

最新文章