Problem 1
Problem 1
Find the sum of all the multiples of 3 or 5 below 1000
For small numbers anything works like the following code
> sum(unique(c(which((1:999)%%3 == 0), which((1:999%%5 == 0))))) [1] 233168 |
But for big numbers
Find the sum of all the multiples of 3 or 5 below 100,000,000 THE COMP IN ALL PROBABILITY HANG .So Whats the alternative To get a more efficient solution you could also calculate the sum of the numbers less than1000 that are divisible by 3, plus the sum of the numbers less than1000 that are divisible by 5. But as you have summed numbers divisible by 15 twice you would have to subtract the sum of the numbers divisible by 15.
Brief Check for 1000
> NumbersDiv <- function(x, N) { + n <- round(N/x) + return(x * n * (n + 1)/2) + } > NumbersDiv(3, 1000) + NumbersDiv(5, 1000) - NumbersDiv(15, 1000) [1] 233163 |
Calculation for 1000000000
> NumbersDiv(3, 1e+09) + NumbersDiv(5, 1e+09) - NumbersDiv(15, + 1e+09) [1] 2.333333e+17 |