(PARI) isok(k) = my(s=[]); fordiv(k, d, s=concat(s, digits(d, 3)); if (fromdigits(s, 3)==k, return(1)); if (fromdigits(s, 3)> k, return(0)));
(Python)
from sympy import divisors
from sympy.ntheory import digits
def ok(n):
target, s = "".join(map(str, digits(n, 3)[1:])), ""
if target[0] != "1": return False
for d in divisors(n):
s += "".join(map(str, digits(d, 3)[1:]))
if len(s) >= len(target): return s == target
elif not target.startswith(s): return False
