Le Monde puzzle [#904.5]
About this #904 arithmetics Le Monde mathematical puzzle:
Find all plural integers, namely positive integers such that (a) none of their digits is zero and (b) removing their leftmost digit produces a dividing plural integer (with the convention that one digit integers are all plural).
a slight modification in the R code allows for a faster exploration, based on the fact that solutions add one extra digit to solutions with one less digit:
First, I found this function on Stack Overflow to turn an integer into its digits:
[sourcecode language=”r” gutter=”false”]
pluri=plura=NULL
#solutions with two digits
for (i in 11:99){
dive=rev(digin(i)[-1])
if (min(dive)>0){
dive=sum(dive*10^(0:(length(dive)-1)))
if (i==((i%/%dive)*dive))
pluri=c(pluri,i)}}
for (n in 2:6){ #number of digits
plura=c(plura,pluri)
pluro=NULL
for (j in pluri){
for (k in (1:9)*10^n){
x=k+j
if (x==(x%/%j)*j)
pluro=c(pluro,x)}
}
pluri=pluro}
[/sourcecode]
which leads to the same output
[sourcecode language=”r” gutter=”false”]
> sort(plura)
[1] 11 12 15 21 22 24 25 31 32 33 35 36
[13] 41 42 44 45 48 51 52 55 61 62 63 64
[25] 65 66 71 72 75 77 81 82 84 85 88 91
[37] 92 93 95 96 99 125 225 312 315 325 375 425
[49] 525 612 615 624 625 675 725 735 825 832 912
[61] 915 925 936 945 975 1125 2125 3125 3375 4125
[70] 5125 5625
[72] 6125 6375 7125 8125 9125 9225 9375 53125
[80] 91125 95625
[/sourcecode]
March 27, 2015 at 11:07 am
Why doesn’t e.g. 714 appear as output? I get 92 numbers under 10^5 with the code:
n <- 1:(1e5)
n <- n[!grepl("0",n)]
a <- expand.grid(first=1:9,second=n)
l <- as.numeric(paste0(a$first,a$second)) %% a$second == 0
sort(as.numeric(paste0(a$first[l],a$second[l])))
March 27, 2015 at 11:24 am
I am alas strongly prejudiced against this number 714…!
More to the point, 714 is not acceptable because 14 is not a multiple of 4. I realise now this constraint is missing from my puzzle, thank you!
March 27, 2015 at 11:26 am
Now thats a nice piece of code :)
March 26, 2015 at 3:07 pm
I like those puzzles. And gave it a chance as well. The advantage of my approach is that it doesn’t use any methods on strings. So there is no conversion or splitting necessary :)
I think its a lot faster. If you have any ideas how to further improve it, let me know :)
uBound = 10000
leMonde = function(uBound) {
j = 1
result = vector()
for(i in 11:uBound) {
l = floor(log10(i))+1
o = sapply(10^(1:(l-1)), function(x) { i %% x } )
if(length(unique(o)) == length(o) & o[1] != 0) {
if(i %% o[(l-1)] == 0) {
result[j] = i
j = j+1
}
}
}
return(result)
}
Best,
Martin
March 26, 2015 at 3:21 pm
In fact my routine isn’t faster XD I assumed it would be. But I guess the sapply term adds more process time than it takes…
March 26, 2015 at 3:33 pm
Thank you for the alternative. Indeed, sapply() is often a front for more loops, so does not gain much time…
March 27, 2015 at 12:04 pm
Update: the text of the puzzle was missing a crucial component, namely the constraint that the dividers themselves are plural. So, e.g., 714 does not work because 4 is not a divider of 14, even though 14 is a divisor of 714…