OFFSET
0,2
COMMENTS
In short: subtract squares when you can, else add nonsquares.
A variant of A282864 (with partial sums A282846) and A282865: In those variants, "square" is replaced by "prime".
The graph is a "superposition" of the nonsquares (on a roughly straight line of slope 1) and the squares in slowly increasingly spaced positions (at indices 0, 2, 4, 7, 10, 14, 17, 21,...: distances 2, 3, 4, 5, 6, 7,... appear 2, 3, 4, 5, 7, 9,... times, respectively).
The graph of the partial (signed) sums yields a nice "moirée" pattern, see A282886.
LINKS
Robert Israel, Table of n, a(n) for n = 0..10000
EXAMPLE
Starting from 0 we cannot subtract the square 1, so we add a(1)=2, then we can subtract a(2)=1. Now we must add nonsquare a(3)=3 before subtracting the square a(4)=4 (to yield zero sum). Now we have to add nonsquares a(5)=5 and a(6)=6 before subtracting the next square, a(7)=9. And so on.
MAPLE
a:= 1: b:= 2:
A[0]:= 0: S:= 0:
for n from 1 to 100 do
if S - a^2 >= 0 then
A[n]:= a^2; S:= S - a^2; a:= a+1;
else A[n]:= b; S:= S + b; b:= b+1;
if issqr(b) then b:= b+1 fi
fi
od:
seq(A[i], i=0..100); # Robert Israel, Apr 15 2019
PROG
(PARI) {print1(a=0); c=1; p=2; for(n=1, 199, print1(", "abs(a+0*if(a<c^2, a+=p; while(issquare(p++), ), a-=c^2; c++)-a)))}
CROSSREFS
KEYWORD
nonn,look
AUTHOR
M. F. Hasler and Eric Angelini, Feb 24 2017
STATUS
approved