Created
October 12, 2015 10:34
-
-
Save anirudhjayaraman/f34a718af26d80c68f90 to your computer and use it in GitHub Desktop.
Solution to [Viral] Math Puzzle for Vietnamese Eight-Year-Olds (Using R)
This file contains hidden or 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),]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment