## 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+13xb:c+d+12xef-11+gxh:i-10=66.

With presumably the operation ordering corresponding to

a+(13xb:c)+d+(12xe)f-11+(gxh:i)-10=66

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

a+(13xb:c)+d+(12xe)f+(gxh:i)=87

and implies that c divides b and i divides gxh. 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+(13*ord/ord)+ord+
12*ord-ord-11+(ord*ord/
ord)-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+ord+12*ord-ord-87)*
ord*ord+13*ord*ord+
ord*ord*ord==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>0) sol=rbind(sol,a)}
sol=sol[do.call(order, as.data.frame(sol)),]
```

and returning the 136 different solutions…

### 6 Responses to “another viral math puzzle”

1. flodel81 Says:

Fun problem. May I suggest a vectorized approach:

library(e1071)
baoloc <- function(ord)
(ord[]+ord[]+12L*ord[]-ord[]-87L)*
ord[]*ord[]+13L*ord[]*ord[]+
ord[]*ord[]*ord[] == 0L

perms <- data.frame(permutations(9))
sol <- perms[baoloc(perms), ]

2. gd047 Says:

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)

• gd047 Says:

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

3. Michael Says:

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.

4. Peter Says:

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.

Kind regards,
Peter

• xi'an Says:

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

```for (t in 1:1e7) baoloc()
```

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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.