Python| sympy.trailing() 方法
在sympy.trailing()方法的帮助下,我们可以计算给定数字的二进制表示中尾随零位的数量,即确定除以该数字的 2 的最大幂。
Syntax:
trailing(n)
Parameter:
n – It denotes the number for which the largest power of 2 that divides that number is determined.
Returns:
Returns the largest power of 2 that divides the given number.
示例 #1:
# import trailing() method from sympy
from sympy.ntheory.factor_ import trailing
n = 64
# Use trailing() method
trailing_n = trailing(n)
print("The largest power of 2 that divides {} is 2^{}.".
format(n, trailing_n))
输出:
The largest power of 2 that divides 64 is 2^6.
示例 #2:
# import trailing() method from sympy
from sympy.ntheory.factor_ import trailing
n = 130
# Use trailing() method
trailing_n = trailing(n)
print("The largest power of 2 that divides {} is 2^{}.".
format(n, trailing_n))
输出:
The largest power of 2 that divides 130 is 2^1.