Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
30-liner: swift solution in Speedy category for End Zeros by przemyslaw.daniel
def div_10(n):
''' fast division by 10'''
q = (n >> 1) + (n >> 2)
q = q + (q >> 4)
q = q + (q >> 8)
q = q + (q >> 16)
q = q >> 3
r = n - (q << 3) - (q << 1)
return q + ((r + 6) >> 4)
def mul_10(n):
''' fast multiplication by 10 '''
return (n << 3) + (n << 1)
def end_zeros(n):
''' count trailing zeros '''
if not n:
return 1
out, t = 0, div_10(n)
while mul_10(t) == n:
n, t = t, div_10(t)
out += 1
return out
March 2, 2020
Comments: