login
A361231
a(1)=2; a(n) is the largest k for which the sum a(n-1) + a(n-2) + ... + a(n-k) is prime; if no such k exists, a(n)=-1.
3
2, 1, 2, 3, 2, 3, 6, 7, 6, 7, 9, 8, 10, 10, 12, 12, 14, 14, 11, 19, 13, 17, 12, 21, 19, 19, 25, 25, 27, 26, 28, 12, 29, 33, 32, 32, 33, 21, 35, 39, 38, 39, 42, 42, 40, 45, 39, 47, 45, 49, 44, 49, 39, 47, 53, 49, 55, 50, 48, 56, 57, 60, 54, 62, 28, 64, 62, 63, 65, 69, 68
OFFSET
1,1
LINKS
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