## another viral math puzzle

After the Singapore Maths Olympiad birthday problem that went viral, here is a Vietnamese primary school puzzle that made the frontline in The Guardian. The question is: *Fill the empty slots with all integers from 1 to 9 for the equality to hold*. In other words, find *a,b,c,d,e,f,g,h,i* such that

*a*+13x*b*:*c*+*d*+12x*e*–*f*-11+*g*x*h*:*i*-10=66.

With presumably the operation ordering corresponding to

*a*+(13x*b*:*c)*+*d*+(12x*e)*–*f*-11+(*g*x*h*:*i)*-10=66

although this is not specified in the question. Which amounts to

*a*+(13x*b*:*c)*+*d*+(12x*e)*–*f*+(*g*x*h*:*i)*=87

and implies that *c* divides *b* and *i* divides *g*x*h*. Rather than pursing this analytical quest further, I resorted to R coding, checking by brute force whether or not a given sequence was working.

baoloc=function(ord=sample(1:9)){ if (ord[1]+(13*ord[2]/ord[3])+ord[4]+ 12*ord[5]-ord[6]-11+(ord[7]*ord[8]/ ord[9])-10==66) return(ord)}

I then applied this function to all permutations of {1,…,9}* [with the help of the perm(combinat) R function]* and found the 128 distinct solutions. Including some for which b:c is not an integer. (Not of this obviously gives a hint as to how a 8-year old could solve the puzzle.)

As pointed out in a comment below, using the test == on scalars is a bad idea—once realising some fractions may be other than integers—and I should thus replace the equality with an alternative that bypasses divisions,

baoloc=function(ord=sample(1:9)){ return(((ord[1]+ord[4]+12*ord[5]-ord[6]-87)* ord[3]*ord[9]+13*ord[2]*ord[9]+ ord[3]*ord[7]*ord[8]==0)*ord)}

leading to the overall R code

sol=NULL perms=as.matrix(data.frame(permutations(9)),ncol=9,byrow=TRUE) for (t in 1:factorial(9)){ a=baoloc(perms[t,]) if (a[1]>0) sol=rbind(sol,a)} sol=sol[do.call(order, as.data.frame(sol)),]

and returning the 136 different solutions…

May 25, 2015 at 8:06 pm

Fun problem. May I suggest a vectorized approach:

library(e1071)

baoloc <- function(ord)

(ord[[1]]+ord[[4]]+12L*ord[[5]]-ord[[6]]-87L)*

ord[[3]]*ord[[9]]+13L*ord[[2]]*ord[[9]]+

ord[[3]]*ord[[7]]*ord[[8]] == 0L

perms <- data.frame(permutations(9))

sol <- perms[baoloc(perms), ]

May 25, 2015 at 8:33 am

Using “==” is not the suggested way to compare scalars. That is why you found 8 less solutions. The distinct solutions are 136. I used the following code:

library(e1071)

library(dplyr)

df <- tbl_df(data.frame(permutations(9)))

df %>%

filter(abs(X1+13*X2/X3+X4+12*X5-X6-

11+X7*X8/X9-10 – 66) % >%

print(n = Inf)

May 25, 2015 at 8:37 am

I don’t know if the code appears correctly in the comment, so I also give it in this link: https://ideone.com/QWKwcv

May 25, 2015 at 8:24 am

IMPORTANT information for those who are scratching their heads and haven’t yet read the code or followed the link through to the article: the colon “:” is a division operator.

May 25, 2015 at 4:45 am

I’m new to R.

Really loved the problem and tried using your function to generate the answers…unfortunately I could not do it myself.

Would appreciate very much if you could send the full code to generate the ansers as well.

my email address is qwwzxx@gmail.com

Thanks in advance.

Kind regards,

Peter

May 25, 2015 at 7:20 am

The easiest solution was to run a massive number of attempts:

while a more elaborate one simply proceeded through the 9! possible permutations of {1,…,9}, but I did not have the leisure to make this exploration clean and efficient so preferred not to post the code.