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


With presumably the operation ordering corresponding to


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


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.

if (ord[1]+(13*ord[2]/ord[3])+ord[4]+
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,


leading to the overall R code

for (t in 1:factorial(9)){
  if (a[1]>0) sol=rbind(sol,a)}

and returning the 136 different solutions…

6 Responses to “another viral math puzzle”

  1. Fun problem. May I suggest a vectorized approach:

    baoloc <- function(ord)
    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:

    df <- tbl_df(data.frame(permutations(9)))
    df %>%
    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

    Thanks in advance.

    Kind regards,

    • 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: Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s