Skip to content

Instantly share code, notes, and snippets.

@niclasnilsson
Created May 21, 2015 06:54
Show Gist options
  • Save niclasnilsson/db63c5110e7a0b53cc98 to your computer and use it in GitHub Desktop.
Save niclasnilsson/db63c5110e7a0b53cc98 to your computer and use it in GitHub Desktop.
The Vietnamese third grade math problem in Ruby
# 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