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[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…

6 Responses to “another viral math puzzle”

  1. 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), ]

  2. 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)

  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. 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

    • 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: