TITLE: Alternating sum of remainders
QUESTION [9 upvotes]: Is there a nice way to evaluate the following for arbitrary positive integers $n,j,L$:
$$\sum_{k=1}^{\left[\frac{n-L}{j}\right]}(-1)^{(n-kj)\pmod L}$$
I feel like this should be easy but, I get confused thinking about the various cases, particularly the greatest-common-divisors of each pair of the three indices.

REPLY [6 votes]: To simplify calculations and space in the proceeding writings we will use the following representation of a fully reduced natural number x modulo L:
$$x \text{ mod L}=x-\text{L}\lfloor \frac{x}{L}\rfloor$$
In addition we will denote your sum with the letter $S$ as a function of your three variables $n,j,L$

To start off we will employ the simplifications just mentioned.
$$S_n(j,L)=\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{(n-kj)\pmod L}=\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{(n-kj)-\text{L}\lfloor \frac{(n-kj)}{L}\rfloor}$$
$$=\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{n+kj+\text{L}\lfloor \frac{n-kj}{L}\rfloor}=(-1)^n\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{kj+\text{L}\lfloor \frac{n-kj}{L}\rfloor}$$
Now we will calculate this final partial sum explicitly for several special cases that can be evaluated more easily. After this we will use these special cases to go back and construct a general formula for the whole expression in all of its entirety.
Case $1$: $j$ even & $L$ even $\implies$ $S_n(j,L)=(-1)^n\lfloor{\frac{n-L}{j}}\rfloor$
Case $2$: $j$ odd & $L$ even $\implies$ $S_n(j,L)=(-1)^n\frac{(-1)^{\lfloor{\frac{n-L}{j}}\rfloor}-1}{2}$
Case $3$: $j$ even & $L$ odd $\implies$ $S_n(j,L)=(-1)^n\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{\lfloor \frac{n-kj}{L}\rfloor}$
Case $4$: $j$ odd & $L$ odd $\implies$ $S_n(j,L)=(-1)^n\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{k+\lfloor \frac{n-kj}{L}\rfloor}$

Now if we take note of the idempotence of the floor function we can see that:
$$\lfloor \frac{n-(\lfloor{\frac{n-L}{j}}\rfloor-k)j}{L}\rfloor=\lfloor \frac{n-(\frac{n-L}{j}-k)j}{L}\rfloor=\lfloor \frac{n-(n-L-kj)}{L}\rfloor=1+\lfloor \frac{kj}{L}\rfloor$$

Using this in conjunction with the simplified cases of $3$ and $4$ gives:
$$\text{Case }3: (-1)^n\sum_{k=0}^{\lfloor{\frac{n-L}{j}}\rfloor-1}(-1)^{\lfloor \frac{n-(\lfloor{\frac{n-L}{j}}\rfloor-k)j}{L}\rfloor}=(-1)^{n-1}\sum_{k=0}^{\lfloor{\frac{n-L}{j}}\rfloor-1}(-1)^{\lfloor \frac{kj}{L}\rfloor}$$
$$\text{Case }4: (-1)^n\sum_{k=0}^{\lfloor{\frac{n-L}{j}}\rfloor-1}(-1)^{\lfloor{\frac{n-L}{j}}\rfloor-k+\lfloor \frac{n-(\lfloor{\frac{n-L}{j}}\rfloor-k)j}{L}\rfloor}=(-1)^{\lfloor{\frac{n-L}{j}}\rfloor+n-1}\sum_{k=0}^{\lfloor{\frac{n-L}{j}}\rfloor-1}(-1)^{k+\lfloor \frac{kj}{L}\rfloor}$$
And now breaking the partial sums up into complete residue systems modulo $L$, in addition to using the previously mentioned fact about the idempotence of the floor function gives us that:
$$\lfloor{\frac{(\lfloor{\frac{n-L}{j}}\rfloor-1)-i}{L}}\rfloor=\lfloor{\frac{\frac{n-L}{j}-1-i}{L}}\rfloor=\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}\rfloor$$
$$\text{Case }3: (-1)^{n-1}\sum_{k=0}^{\lfloor{\frac{n-L}{j}}\rfloor-1}(-1)^{\lfloor \frac{kj}{L}\rfloor}=(-1)^{n-1}\sum_{i=0}^{L-1}\sum_{k=0}^{\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}\rfloor}(-1)^{\lfloor \frac{(Lk+i)j}{L}\rfloor}$$
$$=(-1)^{n-1}\sum_{i=0}^{L-1}\sum_{k=0}^{\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}\rfloor}(-1)^{\lfloor \frac{ij}{L}\rfloor}=(-1)^{n-1}\sum_{i=0}^{L-1}(-1)^{\lfloor \frac{ij}{L}\rfloor}\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}+1\rfloor$$
$$=(-1)^{n-1}\sum_{k=1}^L(-1)^{\lfloor{\frac{(k-1)j}{L}}\rfloor}\lfloor\frac{n-L}{jL}-\frac{k}{L}+1\rfloor$$
Also
$$\text{Case }4: (-1)^{\lfloor{\frac{n-L}{j}}\rfloor+n-1}\sum_{k=0}^{\lfloor{\frac{n-L}{j}}\rfloor-1}(-1)^{k+\lfloor \frac{kj}{L}\rfloor}=(-1)^{\lfloor{\frac{n-L}{j}}\rfloor+n-1}\sum_{i=0}^{L-1}\sum_{k=0}^{\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}\rfloor}(-1)^{Lk+i+\lfloor \frac{(Lk+i)j}{L}\rfloor}$$
$$=(-1)^{\lfloor{\frac{n-L}{j}}\rfloor+n-1}\sum_{i=0}^{L-1}\sum_{k=0}^{\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}\rfloor}(-1)^{i+\lfloor \frac{ij}{L}\rfloor}=(-1)^{\lfloor{\frac{n-L}{j}}\rfloor+n-1}\sum_{i=0}^{L-1}(-1)^{i+\lfloor \frac{ij}{L}\rfloor}\lfloor{\frac{n-L}{jL}-\frac{1+i}{L}}+1\rfloor=(-1)^{n+\lfloor{\frac{n-L}{j}}\rfloor}\sum_{k=1}^L(-1)^{k+\lfloor{\frac{(k-1)j}{L}}\rfloor}\lfloor\frac{n-L}{jL}-\frac{k}{L}+1\rfloor$$

Thus we now have for $S_n(j,L)$:
Case $1$: $j$ even & $L$ even $=(-1)^n\lfloor{\frac{n-L}{j}}\rfloor$
Case $2$: $j$ odd & $L$ even $=(-1)^n\frac{(-1)^{\lfloor{\frac{n-L}{j}}\rfloor}-1}{2}$
Case $3$: $j$ even & $L$ odd $=(-1)^{n-1}\sum_{k=1}^L(-1)^{\lfloor{\frac{(k-1)j}{L}}\rfloor}\lfloor\frac{n-L}{jL}-\frac{k}{L}+1\rfloor$
Case $4$: $j$ odd & $L$ odd $=(-1)^{n+\lfloor{\frac{n-L}{j}}\rfloor}\sum_{k=1}^L(-1)^{k+\lfloor{\frac{(k-1)j}{L}}\rfloor}\lfloor\frac{n-L}{jL}-\frac{k}{L}+1\rfloor$

Now we will adopt Kenneth's Iverson bracket notation to represent indicator functions, while at the same time also taking note that by using his notation we have that if $a$ and $b$ are logical formula and $\wedge$ denotes the logical conjunction operator, then we get that $[a\wedge b]=[a]\times[b]$.
So from this we see that:
[IS Case $1$] = [$j$ even $\wedge$ $L$ even] = [$j$ even]$\times$[$L$ even] = $\frac{(1+(-1)^j)(1+(-1)^L)}{4}$
[IS Case $2$] = [$j$ odd $\wedge$ $L$ even] = [$j$ odd]$\times$[$L$ even] = $\frac{(1-(-1)^j)(1+(-1)^L)}{4}$
[IS Case $3$] = [$j$ even $\wedge$ $L$ odd] = [$j$ even]$\times$[$L$ odd] = $\frac{(1+(-1)^j)(1-(-1)^L)}{4}$
[IS Case $4$] = [$j$ odd $\wedge$ $L$ odd] = [$j$ odd]$\times$[$L$ odd] = $\frac{(1-(-1)^j)(1-(-1)^L)}{4}$
Where the expressions at the end follow from these equalities among indicator functions:
$[n \text{ even}]=\frac{1+(-1)^{n}}{2}$  $\text{ and }$ $[n \text{ odd}]=\frac{1-(-1)^{n}}{2}$

Now after weighting each of the four cases appropriately with their corresponding indicator functions and subsequently adding them all together, alas we finally get the following formula:
$$\sum_{k=1}^{\lfloor{\frac{n-L}{j}}\rfloor}(-1)^{(n-kj)\pmod L}=\frac{(-1)^n(1+(-1)^j)(1+(-1)^L)}{4}\lfloor{\frac{n-L}{j}}\rfloor$$
$$+\frac{(-1)^n(1-(-1)^j)(1+(-1)^L)((-1)^{\lfloor{\frac{n-L}{j}}\rfloor}-1)}{8}$$
$$-\frac{(-1)^{n}(1+(-1)^j)(1-(-1)^L)}{4}\sum_{k=1}^L(-1)^{\lfloor{\frac{(k-1)j}{L}}\rfloor}\lfloor\frac{n-L}{jL}-\frac{k}{L}+1\rfloor$$
$$+\frac{(-1)^{n+\lfloor{\frac{n-L}{j}}\rfloor}(1-(-1)^j)(1-(-1)^L)}{4}\sum_{k=1}^L(-1)^{k+\lfloor{\frac{(k-1)j}{L}}\rfloor}\lfloor\frac{n-L}{jL}-\frac{k}{L}+1\rfloor$$