同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""Binary ExponentiationThis is a method to find a^b in O(log b) time complexity and is one of the most commonlyused methods of exponentiation. The method is also useful for modular exponentiation,when the solution to (a^b) % c is required.To calculate a^b:- If b is even, then a^b = (a * a)^(b / 2)- If b is odd, then a^b = a * a^(b - 1)Repeat until b = 1 or b = 0For modular exponentiation, we use the fact that (a * b) % c = ((a % c) * (b % c)) % c"""def binary_exp_recursive(base: float, exponent: int) -> float:"""Computes a^b recursively, where a is the base and b is the exponent>>> binary_exp_recursive(3, 5)243>>> binary_exp_recursive(11, 13)34522712143931>>> binary_exp_recursive(-1, 3)-1>>> binary_exp_recursive(0, 5)0>>> binary_exp_recursive(3, 1)3>>> binary_exp_recursive(3, 0)1>>> binary_exp_recursive(1.5, 4)5.0625>>> binary_exp_recursive(3, -1)Traceback (most recent call last):...ValueError: Exponent must be a non-negative integer"""if exponent < 0:raise ValueError("Exponent must be a non-negative integer")if exponent == 0:return 1if exponent % 2 == 1:return binary_exp_recursive(base, exponent - 1) * baseb = binary_exp_recursive(base, exponent // 2)return b * bdef binary_exp_iterative(base: float, exponent: int) -> float:"""Computes a^b iteratively, where a is the base and b is the exponent>>> binary_exp_iterative(3, 5)243>>> binary_exp_iterative(11, 13)34522712143931>>> binary_exp_iterative(-1, 3)-1>>> binary_exp_iterative(0, 5)0>>> binary_exp_iterative(3, 1)3>>> binary_exp_iterative(3, 0)1>>> binary_exp_iterative(1.5, 4)5.0625>>> binary_exp_iterative(3, -1)Traceback (most recent call last):...ValueError: Exponent must be a non-negative integer"""if exponent < 0:raise ValueError("Exponent must be a non-negative integer")res: int | float = 1while exponent > 0:if exponent & 1:res *= basebase *= baseexponent >>= 1return resdef binary_exp_mod_recursive(base: float, exponent: int, modulus: int) -> float:"""Computes a^b % c recursively, where a is the base, b is the exponent, and c is themodulus>>> binary_exp_mod_recursive(3, 4, 5)1>>> binary_exp_mod_recursive(11, 13, 7)4>>> binary_exp_mod_recursive(1.5, 4, 3)2.0625>>> binary_exp_mod_recursive(7, -1, 10)Traceback (most recent call last):...ValueError: Exponent must be a non-negative integer>>> binary_exp_mod_recursive(7, 13, 0)Traceback (most recent call last):...ValueError: Modulus must be a positive integer"""if exponent < 0:raise ValueError("Exponent must be a non-negative integer")if modulus <= 0:raise ValueError("Modulus must be a positive integer")if exponent == 0:return 1if exponent % 2 == 1:return (binary_exp_mod_recursive(base, exponent - 1, modulus) * base) % modulusr = binary_exp_mod_recursive(base, exponent // 2, modulus)return (r * r) % modulusdef binary_exp_mod_iterative(base: float, exponent: int, modulus: int) -> float:"""Computes a^b % c iteratively, where a is the base, b is the exponent, and c is themodulus>>> binary_exp_mod_iterative(3, 4, 5)1>>> binary_exp_mod_iterative(11, 13, 7)4>>> binary_exp_mod_iterative(1.5, 4, 3)2.0625>>> binary_exp_mod_iterative(7, -1, 10)Traceback (most recent call last):...ValueError: Exponent must be a non-negative integer>>> binary_exp_mod_iterative(7, 13, 0)Traceback (most recent call last):...ValueError: Modulus must be a positive integer"""if exponent < 0:raise ValueError("Exponent must be a non-negative integer")if modulus <= 0:raise ValueError("Modulus must be a positive integer")res: int | float = 1while exponent > 0:if exponent & 1:res = ((res % modulus) * (base % modulus)) % modulusbase *= baseexponent >>= 1return resif __name__ == "__main__":from timeit import timeita = 1269380576b = 374c = 34runs = 100_000print(timeit(f"binary_exp_recursive({a}, {b})",setup="from __main__ import binary_exp_recursive",number=runs,))print(timeit(f"binary_exp_iterative({a}, {b})",setup="from __main__ import binary_exp_iterative",number=runs,))print(timeit(f"binary_exp_mod_recursive({a}, {b}, {c})",setup="from __main__ import binary_exp_mod_recursive",number=runs,))print(timeit(f"binary_exp_mod_iterative({a}, {b}, {c})",setup="from __main__ import binary_exp_mod_iterative",number=runs,))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。