Created
May 21, 2015 06:54
-
-
Save niclasnilsson/db63c5110e7a0b53cc98 to your computer and use it in GitHub Desktop.
The Vietnamese third grade math problem in Ruby
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 equation | |
def equation(a, b, c, d, e, f, g, h, i) | |
a + 13 * b / c + d + 12 * e - f - 11 + g * h / i - 10 | |
end | |
# The brute force | |
def solutions(*numbers) | |
numbers. | |
permutation. | |
map { |nums| [nums, equation(*nums)] }. | |
select { |nums, res| res == 66 } | |
end | |
# And some printouts | |
def print(solutions) | |
solutions.each { |nums, res| puts "#{nums.inspect} == #{res}" } | |
puts "#{solutions.size} solutions" | |
end | |
print(solutions(1, 2, 3, 4, 5, 6, 7, 8, 9)) | |
print(solutions(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment