login
Lexicographically earliest permutation p of the nonnegative integers such that Sum_{k=0..n} -(-1)^issquare(p(k))*p(k) is nonnegative for all n.
1

%I #9 Mar 12 2017 15:24:07

%S 0,1,4,2,3,9,5,16,6,7,25,8,10,11,36,12,13,14,49,15,17,64,18,19,20,21,

%T 81,22,23,24,100,26,27,28,29,121,30,31,32,33,144,34,35,37,38,169,39,

%U 40,41,42,196,43,44,45,46,225,47,48,50,51,52,256,53,54,55,56,289,57,58,59,60,61,324,62,63,65,66,67,361,68,69,70,71,72,400,73,74,75

%N Lexicographically earliest permutation p of the nonnegative integers such that Sum_{k=0..n} -(-1)^issquare(p(k))*p(k) is nonnegative for all n.

%C In short: subtract nonsquares when you can, else add squares.

%C See A282887 for the partial (signed) sums.

%C A variant of A282840 (with partial sums A282886) and A282846 (corresponding to permutation A282864) and A282865; in the latter variants, "square" is replaced by "prime".

%e Starting from a(0)=0 we add the square a(1)=1, and since we cannot subtract the nonsquare 2, we also add the next larger square a(2)=4. Then we can subtract nonsquares a(3)=2 and a(4)=3. Now we must add the square a(5)=9 before subtracting the nonsquare a(6)=5. Again we must add the next larger square a(7)=16 before subtracting the nonsquares a(8)=6 and a(9)=7. And so on.

%o (PARI) {print1(a=0); c=1; p=2; for(n=1, 199, old=a; if(a>=p, a-=p; while(issquare(p++),), a+=c^2; c++); print1(","abs(old-a))}

%K nonn

%O 0,3

%A _M. F. Hasler_ and _Eric Angelini_, Feb 24 2017