TITLE: Puzzle: Cumulative Sum Divisible by 10
QUESTION [16 upvotes]: If we sum the first $4$ positive integers, we get $4 + 3 + 2 + 1 = 10$, which I think is pretty cool.
I'm interested in seeing solutions to the following puzzle:  If we take the cumulative sum of the first $N$ positive integers, how many times along the way will the sum be divisible by $10$?
In other words, define:
$$f(N) = \left|\left\{n \leq N \;:\; \sum_{i=1}^n i = \frac{n(n+1)}{2} \text{ is divisible by $10$}\right\} \right|$$
Is there a nice expression for $f(N)$?

REPLY [2 votes]: Only the ones digit matters when figuring multiples of ten.  Also, a multiple of ten plus a multiple of ten is a multiple of ten.  Thus, we can find the applicable patterns that we need to consider:
First pattern to get to a multiple of ten (starting with 1):
1+2+3+4 = 10

Second pattern (starting from 5, since we counted 4 last in the previous pattern):
5+6+7+8+9+0+1+2+3+4+5 = 50

Third pattern (starting from 6):
6+7+8+9 = 30

Fourth pattern (starting from 5):
0

Repeat back to pattern 1

Now we have the 4 sequential patterns that we need to consider, which consist of 20 total numbers.  I'm more of a programmer than a mathematician, so here's one way to represent the solution programmatically (in this case using JavaScript), with a working fiddle to see it in action:
function getMultiplesOfTenWhileCountingTo(finalCount){
    //There are 4 multiples every 20 numbers
    var multiples = Math.floor(finalCount/20) * 4;

    //Add the additional multiples based on the remainder
    var remainder = finalCount % 20;
    if(remainder >= 4){
        multiples++;
    } 
    if(remainder >= 15){
        multiples++;
    }
    if(remainder >= 19){
        multiples++;
    }
    return multiples;
}