I am trying to solve the following (Project Euler) problem using R (and iterators and the foreach package),
What is the smallest positive number that is divisible by all of the numbers from one to fifteen?
and while I think that my code should do the job, it does not seem to:
library(foreach)
library(iterators)
# function to check sequence of natural numbers for
# divisibility by a given list of factors
fnDivision = function(maxNum, vFactors) {
foreach(i = icount(factorial(15))) %do% {
if(!i %% 100 ) cat(sprintf("Done with the first %i natural numbers.\n", i))
if(all(! i %% vFactors)) {
return(i)
}
}
}
# test the function
vFactors = c(8, 9, 10, 11, 12, 13, 14, 15)
fnDivision(15, vFactors)
Note that I have reduced the number of factors by which I test the division of the sequence of natural numbers from all the natural numbers from 1-15, to the ones above.
Just in case, the correct answer to this is given in A003418, as 360360, and this
all(! 360360 %% vFactors)
evaluates to TRUE
.