login
Largest number k > 0 such that n - k! is prime, or 0 if no such k exists.
2

%I #20 Jul 27 2020 04:44:58

%S 0,0,1,2,2,1,2,3,3,0,3,1,3,1,2,0,3,1,3,1,2,0,3,1,3,4,4,0,4,1,4,1,2,0,

%T 4,0,4,1,2,0,4,1,4,1,2,0,4,1,3,0,0,0,4,1,4,0,0,0,3,1,4,1,2,0,4,0,4,1,

%U 2,0,4,1,3,1,2,0,4,0,3,1,2,0,4,1,4,0,0,0,3,1,4,0,0,0,4

%N Largest number k > 0 such that n - k! is prime, or 0 if no such k exists.

%C If k > n, n - k! is negative and therefore, not prime.

%H Robert Israel, <a href="/A241424/b241424.txt">Table of n, a(n) for n = 1..10000</a>

%p a:= proc(n) local k, r;

%p r:= 0;

%p for k from 1 do

%p if k! >= n then return r

%p elif isprime(n-k!) then r:= k

%p fi

%p od

%p end proc:

%p seq(a(n),n=1..100); # _Robert Israel_, Aug 10 2014

%t a[n_] := Module[{k, r = 0}, For[k = 1, True, k++, If[k! >= n, Return[r], If[PrimeQ[n - k!], r = k]]]];

%t Array[a, 100] (* _Jean-François Alcover_, Jul 27 2020, after Maple *)

%o (PARI)

%o a(n)=forstep(k=n,1,-1,if(ispseudoprime(n-k!),return(k)))

%o n=1;while(n<150,print1(a(n),", ");n++)

%Y Cf. A175940, A245715.

%K nonn

%O 1,4

%A _Derek Orr_, Aug 08 2014