Year-end appeal: Please make a donation to the OEIS Foundation to support ongoing development and maintenance of the OEIS. We are now in our 61st year, we have over 378,000 sequences, and we’ve reached 11,000 citations (which often say “discovered thanks to the OEIS”).
%I #18 Jul 08 2021 00:46:40
%S 7,7,0,0,89,79,0,0,47,59,0,0,83,0,0,0,0,0,0,0,0,0,0,0,0,809,619,857,0,
%T 227,509,787,823,557,0,907,0,653,0,347,359,0,383,773,0,797,0,0,0,0,
%U 467,479,0,503,0,0,0,0,0,563,0,587,0,0,0,0,0,0,0,0,0,719,0,0,0,0,0,0,0,0
%N Largest k-digit prime == 1 (mod prime(n)) where k is the number of digits in prime(n), or 0 if no such prime exists.
%H Michael S. Branicky, <a href="/A109938/b109938.txt">Table of n, a(n) for n = 1..10000</a>
%e a(13) = 83 as prime(13) = 41 and 83 == 1 (mod 41). 83 is the largest such two-digit prime.
%p A055642 := proc(n) max(1, ilog10(n)+1) ; end: A109938 := proc(n) local p,k,a; p := ithprime(n) ; k := A055642(p) ; a := 0; q := numtheory[pi](10^(k-1)) ; q := ithprime(q+1) ; while A055642(q) < k+1 do if q mod p = 1 and q > a then a := q ; fi ; q := nextprime(q) ; od ; RETURN(a) ; end: seq(A109938(n),n=1..90) ; # _R. J. Mathar_, Aug 17 2007
%o (Python)
%o from sympy import prime, prevprime
%o def a(n):
%o pn = prime(n); k = len(str(pn))
%o p = prevprime(10**k); lb = max(10**(k-1), 2)
%o while p > lb and p%pn != 1: p = prevprime(p)
%o return p if p > lb else 0
%o print([a(n) for n in range(1, 81)]) # _Michael S. Branicky_, Jul 07 2021
%o (Python) # faster version for initial segment of sequence
%o from sympy import prime, primerange
%o def aupto(limit):
%o alst, primeswithkdigs, plimit = [], dict(), prime(limit)
%o for k in range(1, len(str(plimit))+1):
%o primeswithkdigs[k] = list(primerange(10**(k-1), 10**k))[::-1]
%o for pn in primerange(1, plimit+1):
%o k, found = len(str(pn)), False
%o for pk in primeswithkdigs[k]:
%o if pk%pn == 1: alst.append(pk); found = True; break
%o if not found: alst.append(0)
%o return alst
%o print(aupto(80)) # _Michael S. Branicky_, Jul 07 2021
%Y Cf. A109939.
%K base,nonn
%O 1,1
%A _Amarnath Murthy_, Jul 19 2005
%E More terms from _R. J. Mathar_, Aug 17 2007