require 'set' def print (expr) puts "CARD" expr.each do |a| if "#{a}".match(/[\d]/) printf("%s","#{a.to_i} ") end end printf("\n") printf("SOLUTION\n") expr.each do |a| if "#{a}".match(/\d/) printf("%s ","#{a.to_i}") else printf("#{a} ") end end printf("\n\n") end operators = ["+", "-", "*", "/"] checked = Set.new (1..9).each do |a| operators.each do |op1| (1..9).each do |b| operators.each do |op2| (1..9).each do |c| operators.each do |op3| (1..9).each do |d| (0..5).each do |par| temp = [] temp << a << b << c << d; temp.sort! temp = temp.to_s.to_sym unless checked.include? temp expr = [] expr << a.to_f expr << op1 expr << b.to_f expr << op2 expr << c.to_f expr << op3 expr << d.to_f case par when 1 expr[0,0] = "(" expr[4,0] = ")" when 2 expr[4,0] = "(" expr[8,0] = ")" when 3 expr[2,0] = "(" expr[6,0] = ")" when 4 expr[2,0] = "(" expr[8,0] = ")" when 5 expr[0,0] = "(" expr[6,0] = ")" end if (eval("#{expr}") == 24.0) print(expr) checked << temp end end end end end end end end end end