login
A387271
Positive integers whose ternary expansion has equal run-length multisets for digits 1 and 2.
1
5, 7, 11, 15, 19, 21, 29, 33, 44, 45, 50, 55, 57, 63, 70, 76, 83, 87, 99, 104, 116, 132, 135, 140, 142, 146, 150, 163, 165, 171, 178, 189, 194, 196, 208, 210, 220, 228, 245, 249, 261, 266, 290, 297, 302, 304, 308, 312, 332, 348, 377, 395, 396, 401, 405
OFFSET
1,1
COMMENTS
Group the base-3 expansion of m > 0 into runs of equal digits and ignore 0-runs. m is a term if for each k >= 1 the number of 1-runs of length k equals the number of 2-runs of length k.
Sequence is infinite since it is closed under multiplication by powers of 3 (appending trailing zeros).
Invariant under digit-wise swap 1 <-> 2.
LINKS
Ahmet Caglar Saygili, Table of n, a(n) for n = 1..10000
EXAMPLE
395 = (112122)_3. Runs are 11, 2, 1, 22 with lengths [2,1,1,2]. The 1-run multiset is {1,2}; the 2-run multiset is {1,2}. So, 395 is a term.
405 = (120000)_3. Ignoring the trailing 0-run, the nonzero runs are 1, 2 (lengths 1 and 1), so it is also a term.
MATHEMATICA
f[s_, d_] := Sort[Length /@ Select[s, AllTrue[#, #1 == d &] &]]; q[n_] := Module[{s = Split[IntegerDigits[n, 3]]}, f[s, 1] == f[s, 2]]; Select[Range[420], q] (* Amiram Eldar, Sep 14 2025 *)
PROG
(Julia)
function ok(n::Integer)
t = string(n; base=3)
counts1 = Dict{Int, Int}(); counts2 = Dict{Int, Int}(); prev = '0'; cnt = 0
for c in t*'0'
if c == prev
cnt += 1
else
if prev == '1'
counts1[cnt] = get(counts1, cnt, 0) + 1
elseif prev == '2'
counts2[cnt] = get(counts2, cnt, 0) + 1
end
prev = c; cnt = 1
end
end
counts1 == counts2
end
[k for k in 1:405 if ok(k)]
(Python)
from itertools import groupby
from collections import Counter
def ok(n: int) -> bool:
s, m = "", n # s will hold ternary string
while m:
s, m = str(m % 3) + s, m // 3
c1, c2 = Counter(), Counter()
for d, g in groupby(s):
L = sum(1 for _ in g)
if d == '1': c1[L] += 1
elif d == '2': c2[L] += 1
return c1 == c2
[a for a in range(1, 406) if ok(a)]
CROSSREFS
KEYWORD
nonn,base
AUTHOR
STATUS
approved