Skip to content

Instantly share code, notes, and snippets.

@shakemurasan
Created September 17, 2016 08:51
Show Gist options
  • Save shakemurasan/f7ba462af9962d3cd5cf507ff0e6d909 to your computer and use it in GitHub Desktop.
Save shakemurasan/f7ba462af9962d3cd5cf507ff0e6d909 to your computer and use it in GitHub Desktop.
プログラマ脳を鍛える数学パズル:Q2 (別解)
# -- 処理時間計測の自作ライブラリ --
require './process_measure.rb'
# ------------------------------
def print_enable_combinarion(from, to)
s = ['+', '-', '*', '/', '']
(from..to).each do |num|
a = num.to_s.split("")
[a[0]].product(s, [a[1]], s, [a[2]], s, [a[3]]) do |c|
next if abort?(str = c.join)
puts num if eval(str).to_s == num.to_s.reverse
end
end
end
def abort?(str)
(str.size == 4) || (str =~ /[\+\-\*\/]0+[1-9]/) || (str =~ /\/0/)
end
measure_do { print_enable_combinarion(1000, 9999) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment