login
Smallest prime p, distinct from prime(n), that uses exactly the same set of digits as prime(n), and -1 if no such prime exists.
0

%I #42 Apr 01 2026 06:54:26

%S -1,-1,-1,-1,1111111111111111111,31,71,191,223,229,13,73,4111,433,

%T 4447,353,599,661,677,17,37,97,383,8999,79,10111,1013,701,1009,13,271,

%U 13,173,193,419,1151,571,613,617,137,197,811,19,139,179,19,2111,23,277,29,23,293,421,521,2557

%N Smallest prime p, distinct from prime(n), that uses exactly the same set of digits as prime(n), and -1 if no such prime exists.

%C First differs from A358020 at a(11) = 13 != 113 = A358020(11). - _Michael S. Branicky_, Feb 20 2026

%e a(6) = 31, because 13 and 31 use the same set of digits, and no smaller prime distinct from 13 has this property.

%o (PARI) a(n) = if (n<=4, return(-1)); if (n==5, my(p=prime(n), s=Set(digits(p)), q=1); while (!((q!=p) && isprime(q)), q = eval(concat(q, "1"))); return(q)); my(p=prime(n), s=Set(digits(p)), q=2); while(!((Set(digits(q)) == s) && (p!=q)), q=nextprime(q+1)); q; \\ _Michel Marcus_, Feb 19 2026

%o (Python)

%o from sympy import isprime, prime

%o from itertools import count, product

%o def a(n):

%o if n < 5: return -1

%o pn = prime(n)

%o S = sorted(set(str(pn)))

%o for d in count(len(S)):

%o for p in product(S, repeat=d):

%o if p[0] == "0" or len(set(p)) != len(S): continue

%o t = int("".join(p))

%o if t != pn and isprime(t):

%o return t

%o print([a(n) for n in range(1, 56)]) # _Michael S. Branicky_, Feb 20 2026

%Y Cf. A000040, A357096, A358020.

%K sign,base

%O 1,5

%A _Jean-Marc Rebert_, Feb 19 2026