%I #23 Apr 16 2019 01:08:35
%S 0,2,1,3,4,5,6,9,7,8,16,10,11,12,25,13,14,36,15,17,18,49,19,20,21,22,
%T 64,23,24,26,81,27,28,29,30,100,31,32,33,121,34,35,37,38,144,39,40,41,
%U 42,43,169,44,45,46,47,196,48,50,51,52,53,225,54,55,56,57,256,58,59,60,61,62,289,63,65,66,67,68,324,69,70,71,72,73,361,74,75,76
%N Lexicographic-first permutation of the nonnegative integers such that the partial sums (-1)^issquare(a(n))*a(n) always remain nonnegative.
%C In short: subtract squares when you can, else add nonsquares.
%C A variant of A282864 (with partial sums A282846) and A282865: In those variants, "square" is replaced by "prime".
%C 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).
%C The graph of the partial (signed) sums yields a nice "moirée" pattern, see A282886.
%C The variant with opposite signs (subtract nonsquares, add squares) is A282888, with sums A282887.
%H Robert Israel, <a href="/A282840/b282840.txt">Table of n, a(n) for n = 0..10000</a>
%e 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.
%p a:= 1: b:= 2:
%p A[0]:= 0: S:= 0:
%p for n from 1 to 100 do
%p if S - a^2 >= 0 then
%p A[n]:= a^2; S:= S - a^2; a:= a+1;
%p else A[n]:= b; S:= S + b; b:= b+1;
%p if issqr(b) then b:= b+1 fi
%p fi
%p od:
%p seq(A[i],i=0..100); # _Robert Israel_, Apr 15 2019
%o (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)))}
%K nonn,look
%O 0,2
%A _M. F. Hasler_ and _Eric Angelini_, Feb 24 2017