Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
[py3.8+] 1-liner/98 chars with walrus/shifts operators solution in Clear category for Univocalic davasaan by Phil15
davasaan=lambda n:(s:=(v:=(a:=(d:=(n>>1)+(n>>2))+(d>>4))+(a>>8))+(v>>16)>>3)+(n-(((s<<2)+s)<<1)>9)
# def davasaan(n):
# """Approximation of n // 10, which is exact for 0 <= n <= 2000000000."""
# # Clear version - Not a mathematical proof, but a little explanation.
# d = (n >> 1) + (n >> 2) # d = (n // 2) + (n // 4) # near n*(1/2+1/4)
# d += d >> 4 # d += d // 16 # near *= 1+1/16
# d += d >> 8 # d += d // 256 # near *= 1+1/256
# d += d >> 16 # d += d // 65536 # near *= 1+1/65536
# d >>= 3 # d //= 8 # near *= 1/8
# # (1/2+1/4)*(1+1/16)*(1+1/256)*(1+1/65536)*1/8 == 0.09999999997671694
# # With basic maths, we have -1.64215877 < d - n * 0.09999999997671694 <= 0
# r = (((d << 2) + d) << 1) # r == (d * 4 + d) * 2 == d * 10
# return d + (n - r > 9) # d += 1 if n - r > 9 else 0; return d
Dec. 20, 2019
Comments: