I am interested in a more optimized solution for sum of all fizzbuzz numbers in a given range

The fizzbuzz numbers which are less or equal to N are integers between 0 and N that are divisible by 15. Their sum is sum(15*i for i = 1 to N/15) That’s equal to 15*sum(i for i i=1 to N/15), or 15*(N/15)*(1+N/15)/2. (Note, here / means rounding down integer division).

The sum of fizzbuzz numbers in the range [a, b] (that is, numbers greater than or equal to a and less than or equal to b), is equal to the sum of the fizzbuzz numbers less than or equal to b minus the sum of fizzbuzz numbers less than or equal to a-1. That’s 15*(b/15)*(1+b/15)/2 - 15*((a-1)/15)*(1+(a-1)/15)/2 using the result in the first paragraph.

Leave a Comment