This story is popping up on the Interwebs these days:
Empty boxes should be filled with digits from 1 to 9 (without replacement). Important detail: in the puzzle, ‘:’ represents division.
Source material:
Here’s a solution in R.
library(combinat, quietly=T, warn.conflicts=F)
## Warning: package 'combinat' was built under R version 3.1.3
Make a list of all permutations of 1:9.
cands <- permn(9)
Define a function to decide whether a particular permutation is a solution.
test_candidate <- function(c) {
cand <- unlist(c)
if (cand[1] + 13 * cand[2] / cand[3] + cand[4] + 12 * cand[5] - cand[6] - 11 + cand[7] * cand[8] / cand[9] - 10 == 66)
return(TRUE)
return(FALSE)
}
Make a logical vector; each element says whether its list element is a solution.
solutions <- sapply(cands, test_candidate)
How many solutions are there?
sum(solutions)
## [1] 128
Let’s have a look at a few solutions:
head(cands[solutions])
## [[1]]
## [1] 9 1 2 5 6 7 3 4 8
##
## [[2]]
## [1] 7 9 6 1 5 2 3 4 8
##
## [[3]]
## [1] 1 9 6 7 5 2 3 4 8
##
## [[4]]
## [1] 1 5 2 3 4 8 7 9 6
##
## [[5]]
## [1] 1 5 2 3 4 8 9 7 6
##
## [[6]]
## [1] 5 1 2 9 6 7 3 4 8
How are the solutions distributed?
barplot(solutions, axes=F, ylab = "Solution?")
axis(side=2, at=c(0, 1), labels=c("no", "yes"))
I don’t see a pattern, but the order in which permn delivers its results might be hiding something obvious.