Skip to content

Instantly share code, notes, and snippets.

@track8
Created June 10, 2015 15:44
Show Gist options
  • Save track8/86996c6dd6e21201d6b0 to your computer and use it in GitHub Desktop.
Save track8/86996c6dd6e21201d6b0 to your computer and use it in GitHub Desktop.
def formula_ptn(ptn)
ope = ptn.to_s(3).rjust(8, '0').gsub('0', 'n').gsub('1', '+').gsub('2', '-')
(1..9).map(&:to_s).each_with_index.inject('') do |result, (num, index)|
result += num
if ope[index] && ope[index] != 'n'
result += ope[index]
end
result
end
end
0.upto('22222222'.to_i(3)) do |ptn|
f = formula_ptn(ptn)
puts f if eval(f) == 100
end
# 123+45-67+8-9
# 123+4-5+67-89
# 123-45-67+89
# 123-4-5-6-7+8-9
# 12+3+4+5-6-7+89
# 12+3-4+5+67+8+9
# 12-3-4+5-6+7+89
# 1+23-4+56+7+8+9
# 1+23-4+5+6+78-9
# 1+2+34-5+67-8+9
# 1+2+3-4+5+6+78+9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment