# Solution to [Viral] Math Puzzle for Vietnamese Eight-Year-Olds (Using R)

A week back, there was yet another math puzzle that had gone viral, meant for Vietnamese eight-year-olds, a problem that had stumped parents and teachers. You need to fill in the empty boxes below with the digits from 1 to 9 so that the equation makes sense, following the order of operations – multiply first, then division, addition and subtraction last. Apparently, this problem was for third graders in the town of Bao Loc in the Vietnamese Highlands.

I didn’t solve this one with pen and paper, and instead wrote an R program. It’s clearly a problem with a fixed number of variables (nine) associated with standard math operators, meaning there would be 9! (362880) permutations – which makes you think there’s got to be more than one solution. It became obvious I had to write some code.

I wrote a function appropriately named baoloc() to list out the solutions to this problem. The code is self explanatory. I got 128 unique solutions, which means if the students had to solve this problem with pencil on paper, they could get one of 128 possible answers, which makes the job of the examiner arduous and boring!

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 ## The problem could be written as u + 13v/w + x + 12y - z - 11 + pq/r -10 = 66 ## which reduces to u + 13v/w + x + 12y - z + pq/r = 87 ## This problem was solved on [1] "Wed May 27 17:46:52 2015" baoloc <- function() { packages <- rownames(installed.packages()) if("combinat" %in% packages) library("combinat") else install.packages("combinat") numbers <- 1:9 permutations <- permn(numbers) ## list of all permutations of vector input solutions <- numeric(9) for(i in 1:length(permutations)) { solution <- permutations[[i]] if(solution[1] + 13*(solution[2]/solution[3]) + solution[4] + 12*solution[5] - solution[6] + (solution[7]*solution[8] / solution[9]) == 87) { solutions <- rbind(solutions, solution) } } print("The number of solutions are:") print(nrow(solutions)-1) return(solutions[2:nrow(solutions),]) }
view raw baoloc.R hosted with ❤ by GitHub

The above code produces the following solutions:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 [1] "The number of solutions are:" [1] 128 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] solution 9 1 2 5 6 7 3 4 8 solution 7 9 6 1 5 2 3 4 8 solution 1 9 6 7 5 2 3 4 8 solution 1 5 2 3 4 8 7 9 6 solution 1 5 2 3 4 8 9 7 6 solution 5 1 2 9 6 7 3 4 8 solution 5 1 2 9 6 7 4 3 8 solution 1 5 2 8 4 7 3 9 6 solution 1 5 2 8 4 7 9 3 6 solution 1 9 6 7 5 2 4 3 8 solution 7 9 6 1 5 2 4 3 8 solution 9 1 2 5 6 7 4 3 8 solution 1 2 6 4 7 8 5 3 9 solution 1 2 6 4 7 8 3 5 9 solution 1 4 8 2 7 9 3 5 6 solution 1 4 8 2 7 9 5 3 6 solution 7 1 4 9 6 5 2 3 8 solution 9 1 4 7 6 5 2 3 8 solution 5 4 1 9 2 7 8 3 6 solution 5 4 1 9 2 7 3 8 6 solution 9 4 1 5 2 7 8 3 6 solution 9 4 1 5 2 7 3 8 6 solution 4 9 6 1 5 8 3 7 2 solution 4 9 6 1 5 8 7 3 2 solution 8 6 4 7 5 9 1 3 2 solution 7 6 4 8 5 9 1 3 2 solution 9 4 8 5 6 7 1 3 2 solution 5 4 8 9 6 7 1 3 2 solution 1 9 6 4 5 8 7 3 2 solution 1 9 6 4 5 8 3 7 2 solution 9 1 4 7 6 5 3 2 8 solution 7 1 4 9 6 5 3 2 8 solution 1 3 9 4 7 8 2 5 6 solution 1 3 4 7 6 5 2 9 8 solution 1 3 4 7 6 5 9 2 8 solution 1 3 9 4 7 8 5 2 6 solution 1 5 3 9 4 2 7 8 6 solution 1 5 3 9 4 2 8 7 6 solution 1 3 2 9 5 6 7 4 8 solution 1 3 2 9 5 6 4 7 8 solution 1 3 6 2 7 9 5 4 8 solution 1 3 6 2 7 9 4 5 8 solution 1 3 2 4 5 8 7 9 6 solution 1 3 2 4 5 8 9 7 6 solution 6 3 1 9 2 5 8 7 4 solution 6 3 1 9 2 5 7 8 4 solution 9 3 1 6 2 5 8 7 4 solution 9 3 1 6 2 5 7 8 4 solution 7 3 1 5 2 6 8 9 4 solution 7 3 1 5 2 6 9 8 4 solution 5 3 1 7 2 6 9 8 4 solution 5 3 1 7 2 6 8 9 4 solution 9 5 3 1 4 2 7 8 6 solution 9 5 3 1 4 2 8 7 6 solution 3 1 4 2 7 9 6 5 8 solution 3 1 4 2 7 9 5 6 8 solution 7 3 4 1 6 5 9 2 8 solution 7 3 4 1 6 5 2 9 8 solution 3 6 4 9 5 8 1 7 2 solution 3 6 4 9 5 8 7 1 2 solution 5 4 8 9 6 7 3 1 2 solution 9 4 8 5 6 7 3 1 2 solution 7 6 4 8 5 9 3 1 2 solution 8 6 4 7 5 9 3 1 2 solution 9 6 4 3 5 8 1 7 2 solution 9 6 4 3 5 8 7 1 2 solution 4 3 9 1 7 8 5 2 6 solution 4 3 9 1 7 8 2 5 6 solution 4 3 2 1 5 8 9 7 6 solution 4 3 2 1 5 8 7 9 6 solution 3 2 4 8 5 1 7 9 6 solution 3 2 4 8 5 1 9 7 6 solution 5 9 3 6 2 1 8 7 4 solution 5 9 3 6 2 1 7 8 4 solution 3 5 2 1 4 8 9 7 6 solution 3 5 2 1 4 8 7 9 6 solution 6 9 3 5 2 1 7 8 4 solution 6 9 3 5 2 1 8 7 4 solution 3 9 6 2 5 1 7 4 8 solution 3 9 6 2 5 1 4 7 8 solution 3 2 8 6 5 1 7 9 4 solution 3 2 8 6 5 1 9 7 4 solution 7 3 2 8 5 9 6 1 4 solution 8 3 2 7 5 9 6 1 4 solution 8 3 2 7 5 9 1 6 4 solution 7 3 2 8 5 9 1 6 4 solution 3 2 1 5 4 7 8 9 6 solution 3 2 1 5 4 7 9 8 6 solution 3 9 2 8 1 5 7 6 4 solution 9 3 2 1 5 6 7 4 8 solution 3 9 2 8 1 5 6 7 4 solution 9 3 2 1 5 6 4 7 8 solution 2 3 6 1 7 9 4 5 8 solution 2 3 6 1 7 9 5 4 8 solution 8 9 2 3 1 5 6 7 4 solution 8 9 2 3 1 5 7 6 4 solution 2 9 6 3 5 1 4 7 8 solution 2 9 6 3 5 1 7 4 8 solution 6 2 8 3 5 1 9 7 4 solution 6 2 8 3 5 1 7 9 4 solution 7 2 8 9 6 5 3 1 4 solution 9 2 8 7 6 5 3 1 4 solution 8 7 2 5 3 9 6 1 4 solution 8 7 2 5 3 9 1 6 4 solution 5 7 2 8 3 9 1 6 4 solution 5 7 2 8 3 9 6 1 4 solution 8 2 4 3 5 1 9 7 6 solution 8 2 4 3 5 1 7 9 6 solution 8 5 2 7 4 9 3 1 6 solution 7 5 2 8 4 9 3 1 6 solution 4 2 6 1 7 8 3 5 9 solution 4 2 6 1 7 8 5 3 9 solution 7 5 2 8 4 9 1 3 6 solution 8 5 2 7 4 9 1 3 6 solution 2 4 8 1 7 9 5 3 6 solution 2 8 6 9 4 1 5 7 3 solution 2 8 6 9 4 1 7 5 3 solution 9 8 6 2 4 1 7 5 3 solution 9 8 6 2 4 1 5 7 3 solution 2 4 8 1 7 9 3 5 6 solution 2 1 4 3 7 9 5 6 8 solution 2 1 4 3 7 9 6 5 8 solution 8 5 2 1 4 7 9 3 6 solution 8 5 2 1 4 7 3 9 6 solution 5 2 1 3 4 7 9 8 6 solution 5 2 1 3 4 7 8 9 6 solution 9 2 8 7 6 5 1 3 4 solution 7 2 8 9 6 5 1 3 4