Python | sympy.trailing () method

Python Methods and Functions

Using the sympy.trailing () method, we can count the number of trailing zeros in the binary representation of a given number, that is, determine the greatest power of 2 that divides this number.

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.

Example # 1:

# import trailing () method from sympy

from sympy.ntheory.factor_ import trailing

 

n = 64

 
# Use the trailing () method

trailing_n = trailing (n) 

 

print ( "The largest power of 2 that divides {} is 2 ^ {}."

format (n, trailing_n) )

Output:

 The largest power of 2 that divides 64 is 2 ^ 6. 

Example # 2:

# import trailing () method from sympy

from sympy.ntheory.factor_ import trailing

 

n = 130

 
# Use the trailing () method

trailing_n = trailing (n) 

 

print ( " The largest power of 2 that divides {} is 2 ^ {}. " .

  format (n, trailing_n))

Exit:

 The largest power of 2 that divides 130 is 2 ^ one. 




Get Solution for free from DataCamp guru