A301574 a(n) = distance from n to nearest 3-smooth number (A003586).
0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 3, 2, 1, 0, 1, 1, 0, 1, 2, 2, 1, 0, 1, 2, 1, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6
Offset: 1
Examples
a(20) = a(22) = 2 because 18 is the nearest 3-smooth number to 20 and 24 is the nearest 3-smooth number to 22.
Links
Programs
-
PARI
\\ See Links section.
-
Python
from sympy import integer_log def A301574(n): def bisection(f,kmin=0,kmax=1): while f(kmax) > kmax: kmax <<= 1 while kmax-kmin > 1: kmid = kmax+kmin>>1 if f(kmid) <= kmid: kmax = kmid else: kmin = kmid return kmax def f(x): return x-sum((x//3**i).bit_length() for i in range(integer_log(x,3)[0]+1)) k = n-f(n) return min(n-bisection(lambda x:f(x)+k,k,k),bisection(lambda x:f(x)+k+1,n,n)-n) # Chai Wah Wu, Oct 22 2024
Comments