Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
29-liner: no division and mutiplication solution in Speedy category for Max Digit 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 max_digit(n):
''' finding max digit '''
result = 0
while n:
p = div_10(n)
q = mul_10(p)
result = max(result, n-q)
n = p
return result
March 3, 2020
Comments: