login
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

%I #24 Mar 08 2023 10:30:13

%S 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,

%T 25,27,26,28,12,29,33,32,32,33,21,35,39,38,39,42,42,40,45,39,47,45,49,

%U 44,49,39,47,53,49,55,50,48,56,57,60,54,62,28,64,62,63,65,69,68

%N 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.

%H Neal Gersh Tolunsky, <a href="/A361231/b361231.txt">Table of n, a(n) for n = 1..10000</a>

%e 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.

%p R:= 2: S:= [0,2];

%p for n from 2 to 100 do

%p found:= false;

%p for k from n-1 to 1 by -1 do

%p if isprime(S[n]-S[n-k]) then

%p found:= true; break

%p fi

%p od;

%p if not found then k:= -1 fi;

%p R:= R,k; S:= [op(S),S[n]+k];

%p od:

%p R; # _Robert Israel_, Mar 06 2023

%t 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 *)

%o (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

%Y Cf. A000040, A361178, A361199.

%K nonn

%O 1,1

%A _Neal Gersh Tolunsky_, Mar 05 2023