Here is a shortcut that fulfills this amount, which is probably more in the spirit of the problem:
3*(333*334/2) + 5*(199*200/2) - 15*(66*67/2) ## [1] 233168
This is why it works:
The set of integers [1,999] contains:
333, which are divided by 3. Their sum is 3*sum(1:333) or 3*(333*334/2) .
199 values, which are divided by 5. Their sum is 5*sum(1:199) or 5*(199*200/2) .
Adding these values ββgives a number that is too large at their intersection, which are values ββthat are divided by 15. There are 66 such values, and their sum is 15*(1:66) or 15*(66*67/2) 15*(1:66) 15*(66*67/2)
As a function of N, this can be written:
f <- function(N) { threes <- floor(N/3) fives <- floor(N/5) fifteens <- floor(N/15) 3*(threes*(threes+1)/2) + 5*(fives*(fives+1)/2) - 15*(fifteens*(fifteens+1)/2) }
Donation:
f(999) ## [1] 233168 f(99) ## [1] 2318
Matthew lundberg
source share