May 27, 2014, 9:25 PM
# The German geek podcast Fanboys asked their audience in their latest episode 135 to check which of the episode numbers for upcoming shows will be a 2-level Harshad Number.
# A Harshad Number in a given number base, is an integer that is divisible by the sum of its digits when written in that base.
# A 2-level Harshad Number, according to the Fanboys interpretation, shall be a number which divisible by the sum of its digits, and the resulting ratio itself shall be a Harshad Number.
# http://stackoverflow.com/questions/18674535/function-for-multi-level-harshad-number-in-r
multi.step.harshed.number = function(numlevels, start, end) {
library(parallel)
processors= detectCores()
cl= makeCluster(processors)
is.integer= function(x) ifelse (x== as.integer(x), return (x), return(NA))
digitsum = function(x) sum(floor(x/10^(0:(nchar(x)-1))) %% 10)
digitsumratio= function(x) ifelse(is.integer(x/digitsum(x)), x/digitsum(x), NA)
multi.digitsumratio= function(iter, x) {
for (i in 1:iter) x= digitsumratio(x)
return (ifelse(is.na(x), FALSE, TRUE))
}
sequence=start:end
idx=parSapply(cl=cl,X=sequence, FUN=function (x) multi.digitsumratio(numlevels, x))
sequence[idx]
}
multi.step.harshed.number(42, 100, 10000)
[1] 100 108 120 162 180 200 210 216 240 243 270
[12] 300 324 360 378 400 405 420 432 450 480 486
[23] 500 540 600 630 648 700 720 756 800 810 840
[34] 864 900 972 1000 1080 1200 1296 1458 1620 1800 1944
[45] 2000 2100 2160 2400 2430 2700 2916 3000 3240 3402 3600
[56] 3780 4000 4050 4200 4320 4374 4500 4800 4860 5000 5400
[67] 5832 6000 6300 6480 6804 7000 7200 7290 7560 8000 8100
[78] 8400 8640 8748 9000 9720 10000