OFFSET
1,1
LINKS
Neal Gersh Tolunsky, Table of n, a(n) for n = 1..10000
EXAMPLE
To find a(6), we look at the terms so far (2,1,2,3,2) and add them beginning with the most recent terms, seeking a prime sum. (2+3+2)=7 is produced by the largest number of terms (3), so a(6)=3.
MAPLE
R:= 2: S:= [0, 2];
for n from 2 to 100 do
found:= false;
for k from n-1 to 1 by -1 do
if isprime(S[n]-S[n-k]) then
found:= true; break
fi
od;
if not found then k:= -1 fi;
R:= R, k; S:= [op(S), S[n]+k];
od:
R; # Robert Israel, Mar 06 2023
MATHEMATICA
a[1] = 2; a[n_] := a[n] = Module[{s = Sum[a[i], {i, 1, n - 1}], m = 1}, While[s > 0 && ! PrimeQ[s], s -= a[m]; m++]; If[s == 0, -1, n - m]]; Array[a, 100] (* Amiram Eldar, Mar 06 2023 *)
PROG
(PARI) { t = 0; for (n = 1, #a = vector(71), if (n==1, a[n] = 2, a[n] = -1; p = t; for (i=1, n-1, if (isprime(p), a[n] = n-i; break, p -= a[i]; ); ); ); t += a[n]; print1 (a[n]", "); ); } \\ Rémy Sigrist, Mar 06 2023
CROSSREFS
KEYWORD
nonn
AUTHOR
Neal Gersh Tolunsky, Mar 05 2023
STATUS
approved