%I #25 Aug 07 2023 19:04:22
%S 2,3,5,7,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
%T 101,103,107,109,131,151,163,173,181,191,193,197,241,251,263,271,281,
%U 283,293,307,313,317,353,373,383,397,401,409,419,439,461,463,487,491
%N Primes in which digits alternately rise and fall (or vice versa); sometimes called undulating primes.
%D C. A. Pickover, "Wonders of Numbers", Oxford New York 2001, Chapter 52, pp. 123-124, 316-317.
%H Robert Israel, <a href="/A059168/b059168.txt">Table of n, a(n) for n = 1..10000</a>
%H C. A. Pickover, "Wonders of Numbers, Adventures in Mathematics, Mind and Meaning," <a href="http://www.zentralblatt-math.org/zmath/en/search/?q=an:0983.00008&format=complete">Zentralblatt review</a>
%p extend:= proc(n) local L,j;
%p L:= convert(n,base,10);
%p if (L[-1] < L[-2]) xor (nops(L)::odd) then
%p seq(10*n+j,j=0..L[1]-1)
%p else
%p seq(10*n+j,j=L[1]+1..9)
%p fi
%p end proc:
%p und[2]:= [seq(seq(10*i+j,j=subs(i=NULL,[$0..9])),i=1..9)]:
%p for i from 3 to 4 do und[i]:= map(extend,und[i-1]) od:
%p select(isprime, [2,3,5,7,seq(op(und[i],i=2..4)]); # _Robert Israel_, Nov 15 2018
%t d[n_]:=Differences[IntegerDigits[n]]; mQ[n_]:=MemberQ[d[n],0]==False; a[n_]:=DeleteDuplicates[Sign[Take[d[n],{1,-1,2}]]]; b[n_]:=DeleteDuplicates[Sign[Take[d[n],{2,-1,2}]]]; t={}; Do[p=Prime[n]; If[mQ[p],If[Length[IntegerDigits[p]]<=2,AppendTo[t,p],If[Length[a[p]]==Length[b[p]]==1 && a[p][[1]]!=b[p][[1]],AppendTo[t,p]]]],{n,95}]; t (* _Jayanta Basu_, May 08 2013 *)
%t Table[Which[p<10,p,p<100&&Differences[IntegerDigits[p]]!={0},p,p>100&&Union[Total/@ Partition[Sign[Differences[IntegerDigits[p]]],2,1]]=={0},p,True,Nothing],{p,Prime[ Range[ 150]]}] (* _Harvey P. Dale_, Aug 07 2023 *)
%o (Python)
%o from sympy import isprime
%o def f(w,dir):
%o if dir == 1:
%o for s in w:
%o for t in range(int(s[-1])+1,10):
%o yield s+str(t)
%o else:
%o for s in w:
%o for t in range(0,int(s[-1])):
%o yield s+str(t)
%o A059168_list = []
%o for l in range(5):
%o for d in '123456789':
%o x = d
%o for i in range(1,l+1):
%o x = f(x,(-1)**i)
%o A059168_list.extend([int(p) for p in x if isprime(int(p))])
%o if l > 0:
%o y = d
%o for i in range(1,l+1):
%o y = f(y,(-1)**(i+1))
%o A059168_list.extend([int(p) for p in y if isprime(int(p))]) # _Chai Wah Wu_, Apr 25 2021
%Y Cf. A032758, A059170.
%K nonn,base,easy
%O 1,1
%A _N. J. A. Sloane_, Feb 14 2001
%E More terms from Larry Reeves (larryr(AT)acm.org), Feb 15 2001
%E Offset changed by _Robert Israel_, Nov 15 2018