Solution:
Let Sn=∑k=1nf(k). We want the value of S2018.
Claim: f(n)={n−n if Sn−1<n if Sn−1≥n
Proof: The inequality condition is 0≤Sn−1+f(n)<2n.
- If n>Sn−1, then 0≤Sn−1+f(n)<n+f(n) so f(n)>−n. Therefore, f(n)=n.
- If n≤Sn−1, then n+f(n)≤Sn−1+f(n)<2n so f(n)<n. Therefore, f(n)=−n.
Claim: If Sn=0, then
where
j=1,2,…,n+1.
The pattern here: Sn+1,Sn+3,Sn+5,… are numbers decreasing by 1, while Sn+2,Sn+4,Sn+6,… are numbers increasing by 1.
Proof: Sn=0<n so f(n+1)=n+1. Thus, Sn+1=0+(n+1)=n+1.
Sn+1=n+1<n+2 so f(n+2)=n+2. Thus, Sn+2=n+1+(n+2)=2n+3.
Sn+2=2n+3>n+3 so f(n+3)=−n−3. Thus, Sn+3=2n+3+(−n−3)=n.
We prove the claim by strong induction. Suppose the pattern holds for Sn+1,Sn+2,…,Sn−1+2j.
Since Sn−1+2j=Sn+1+2(j−1)=n+1−(j−1)=n+2−j<n+2j, then f(n+2j)=n+2j so Sn+2j=(n+2−j)+(n+2j)=2n+2+j.
On the other hand, since Sn+2j=2n+2+j=(n+2j+1)+(n+1−j)≥n+2j+1, then f(n+2j+1)=−(n+2j+1) so Sn+2j+1=(2n+2+j)−(n+2j+1)=n+1−j, which proves the claim.
Eventually, Sn+1,Sn+3,…,Sn+2j+1,… will decrease to 0, when j=n+1. Thus, if Sn=0, it follows that the next 0 value is S3(n+1).
Therefore, S3=0, S3⋅4=S12=0, S3⋅13=S39=0, S3⋅40=S120=0, S3⋅121=S363=0, S3⋅364=S1092=0.
Since 2018=1092+2⋅463, then S2018=2⋅1092+2+463=2649.