OFFSET
0,3
COMMENTS
In other words, the k-th ternary digit of a(n) is congruent (modulo 3) to the alternate sum of the digits to the left of (and including) the k-th ternary digit of n.
LINKS
EXAMPLE
For n = 42: the ternary expansion of 42 is "1120"; also:
+ 1 = 1 (mod 3)
- 1 + 1 = 0 (mod 3)
+ 1 - 1 + 2 = 2 (mod 3)
- 1 + 1 - 2 + 0 = 1 (mod 3)
- so the ternary expansion of a(42) is "1021", and a(42) = 34.
PROG
(PARI) a(n, base = 3) = { my (d = digits(n, base), s = 0); for (i = 1, #d, d[i] = (s = d[i]-s) % base; ); fromdigits(d, base); }
(Python)
from itertools import accumulate
from sympy.ntheory import digits
def A370932(n):
t = accumulate(((-j if i&1 else j) for i, j in enumerate(digits(n, 3)[1:])), func=lambda x, y: (x+y)%3)
return int(''.join(str(-d%3 if i&1 else d) for i, d in enumerate(t)), 3) # Chai Wah Wu, Mar 08 2024
CROSSREFS
KEYWORD
AUTHOR
Rémy Sigrist, Mar 06 2024
STATUS
approved