Skip to content

Instantly share code, notes, and snippets.

@makevoid
Last active September 22, 2015 14:44
Show Gist options
  • Select an option

  • Save makevoid/dcf4736f8de7083323c6 to your computer and use it in GitHub Desktop.

Select an option

Save makevoid/dcf4736f8de7083323c6 to your computer and use it in GitHub Desktop.
Fun math operators challenge solved with programming
# problem: figure out the operations
#
# expression:
#
# "7 # 7 # 77 # 7 # 7 = 497"
# I'm not good at math so I made this program to solve the problem
operators = %w(+ - * /)
values = [7, 7, 77, 7, 7]
expected_result = 497
loop do
operations = ["+"]
(values.length-1).times do
operations << operators.sample
end
result = 0
values.each_with_index do |val, idx|
result = result.send operations[idx], val
end
if result == expected_result
puts "\noperations are: #{operations[1..-1].inspect}"
break
end
print "."
end
# run this program to see the result