login
The OEIS is supported by the many generous donors to the OEIS Foundation.

 

Logo
Hints
(Greetings from The On-Line Encyclopedia of Integer Sequences!)
A081134 Distance to nearest power of 3. 10
0, 1, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7 (list; graph; refs; listen; history; text; internal format)
OFFSET
1,5
LINKS
Hsien-Kuei Hwang, S. Janson, and T.-H. Tsai, Exact and Asymptotic Solutions of a Divide-and-Conquer Recurrence Dividing at Half: Theory and Applications, ACM Transactions on Algorithms, 13:4 (2017), #47; DOI: 10.1145/3127585.
FORMULA
a(n) = min(n-3^floor(log(n)/log(3)), 3*3^floor(log(n)/log(3))-n).
From Peter Bala, Sep 30 2022: (Start)
a(n) = n - A006166(n); a(n) = 2*n - A003605(n).
a(1) = 0, a(2) = 1, a(3) = 0; thereafter, a(3*n) = 3*a(n), a(3*n+1) = 2*a(n) + a(n+1) and a(3*n+2) = a(n) + 2*a(n+1). (End)
EXAMPLE
a(7) = 2 since 9 is closest power of 3 to 7 and 9 - 7 = 2.
MAPLE
a:= n-> (h-> min(n-h, 3*h-n))(3^ilog[3](n)):
seq(a(n), n=1..100); # Alois P. Heinz, Mar 28 2021
MATHEMATICA
Flatten[Table[Join[Range[0, 3^n], Range[3^n-1, 1, -1]], {n, 0, 4}]] (* Harvey P. Dale, Dec 31 2013 *)
PROG
(PARI) a(n) = my (p=#digits(n, 3)); return (min(n-3^(p-1), 3^p-n)) \\ Rémy Sigrist, Mar 24 2018
(Python)
def A081134(n):
kmin, kmax = 0, 1
while 3**kmax <= n:
kmax *= 2
while True:
kmid = (kmax+kmin)//2
if 3**kmid > n:
kmax = kmid
else:
kmin = kmid
if kmax-kmin <= 1:
break
return min(n-3**kmin, 3*3**kmin-n) # Chai Wah Wu, Mar 31 2021
CROSSREFS
Sequence in context: A165033 A179766 A342872 * A017848 A108619 A091327
KEYWORD
easy,nonn
AUTHOR
Klaus Brockhaus, Mar 08 2003
STATUS
approved

Lookup | Welcome | Wiki | Register | Music | Plot 2 | Demos | Index | Browse | More | WebCam
Contribute new seq. or comment | Format | Style Sheet | Transforms | Superseeker | Recents
The OEIS Community | Maintained by The OEIS Foundation Inc.

License Agreements, Terms of Use, Privacy Policy. .

Last modified March 19 04:58 EDT 2024. Contains 370952 sequences. (Running on oeis4.)