%I #26 Mar 08 2020 00:04:17
%S 2,23,101,2129,10103,210101,1010129,21010127,101010167,2101010147,
%T 10101010163,210101010187,1010101010341,21010101010147,
%U 101010101010323,2101010101010141,10101010101010141,210101010101010323,1010101010101010143
%N Smallest n-digit prime with property that digits alternate in parity.
%H Robert Israel, <a href="/A068876/b068876.txt">Table of n, a(n) for n = 1..700</a>
%e a(4) = 2129 as 2, 1, 2 and 9 have even and odd parity alternately.
%p alp:= proc(n) local L,d;
%p L:= convert(n,base,10);
%p d:= nops(L);
%p if d::even then L:= L + map(op, [[0,1]$(d/2)]) else L:= L + map(op, [[0,1]$((d-1)/2),[0]]) fi;
%p nops(convert(L mod 2, set))=1
%p end proc:
%p f:= proc(d) local s;
%p if d::even then s:= 2*10^(d-1)+(10^d-1)/99-1
%p else s:= (10^(d+1)-1)/99-1
%p fi;
%p do s:= nextprime(s);
%p if alp(s) then return s fi
%p od
%p end proc:
%p seq(f(d),d=1..20); # _Robert Israel_, Aug 14 2018
%t fQ[n_] := Block[{m = Mod[ IntegerDigits@ n, 2]}, m == Split[m, UnsameQ][[1]]]; f[n_] := Block[{c = 1 + 100 (100^Ceiling[n/2 - 1] - 1)/99, k}, k = If[ OddQ@ n, c, 2*10^(n - 1) + c]; k = NextPrime[k - 1]; While[ !fQ@ k, k = NextPrime@ k]; k]; Array[f, 21] (* _Robert G. Wilson v_, Apr 01 2011 *)
%o (Sage)
%o concat = lambda x: Integer(''.join(map(str,x)),base=10)
%o def A068876(n):
%o dd = {0:range(0,10,2), 1: range(1,10,2)}
%o for d0 in [1..9]:
%o if n % 2 == 0 and d0 % 2 == 1: continue # optimization
%o ds = [dd[(d0+1+i) % 2] for i in range(n-1)]
%o for dr in cartesian_product(ds):
%o c = concat([d0]+dr)
%o if is_prime(c): return c # _D. S. McNeil_, Apr 02 2011
%Y Cf. A030144, A068877, A056830.
%K nonn,base
%O 1,1
%A _Amarnath Murthy_, Mar 19 2002
%E a(9)-a(13) corrected and a(14)-a(19) from _Donovan Johnson_, Apr 01 2011