more games of life
Another puzzle in memoriam of John Conway in The Guardian:
Find the ten digit number, abcdefghij. Each of the digits is different, and
- a is divisible by 1
- ab is divisible by 2
- abc is divisible by 3
- abcd is divisible by 4
- abcde is divisible by 5
- abcdef is divisible by 6
- abcdefg is divisible by 7
- abcdefgh is divisible by 8
- abcdefghi is divisible by 9
- abcdefghij is divisible by 10
Which brute force R coding by checking over random permutations of (1,2,…,9) [since j=0] solves within seconds:
while(0<1) if (prod(!(x<-sum(10^{0:8}*sample(1:9)))%/%10^{7:0}%%2:9))break()
into x=3816547290. And slightly less brute force R coding even faster:
while(0<1){ e=sample(c(2,6,8))#even o=sample(c(1,3,7,9))#odd if((!(o[1]+e[1]+o[2])%%3)& (!(10*o[2]+e[2])%%4)& (!(o[1]+e[1]+o[2]+e[2]+5+4)%%3)& (!sum(10^{6:0}*c(o[1],e[1],o[2],e[2],5,4,o[3]))%%7)& (!(10*o[3]+e[3])%%8)& (!(sum(o)+sum(e))%%9)){ print(sum(10^{9:0}*c(o[1],e[1],o[2],e[2],4,5,o[3],e[3],o[4],0)));break()}}
May 6, 2020 at 6:50 am
Another approach without random sampling
library(tidyverse)
library(gtools)
df <- permutations(9, 9, v = 1:9)
df <- as.data.frame(df)
names(df) %
mutate(j = 0) %>%
filter(e == 5) %>%
filter_at(vars(b, d, f, h), ~ . %% 2 == 0) %>%
mutate(
cd = paste0(c, d) %>% as.numeric(),
fgh = paste0(f, g, h) %>% as.numeric(),
abcdefg = paste0(a, b, c, d, e, f, g) %>% as.numeric()
) %>%
filter((a + b + c) %% 3 == 0) %>%
filter((d + e + f) %% 3 == 0) %>%
filter(fgh %% 8 == 0) %>%
filter(abcdefg %% 7 == 0) %>%
filter(cd %% 4 == 0) %>%
select(-c(cd, fgh, abcdefg))
May 6, 2020 at 6:59 am
The code did not paste correctly. Here is an image of it https://ibb.co/X75V4pt
May 6, 2020 at 8:54 am
Thanks a lot, I alas cannot test it as tidyverse does not install.
May 6, 2020 at 8:59 am
You could test it here https://rdrr.io/snippets/
May 6, 2020 at 9:05 am
Thanks again!
May 5, 2020 at 8:16 am
[…] article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]