知恵袋の数学の質問のゾロ目の問題をRubyで解いてみた。

 知恵袋の数学の質問のゾロ目の問題Rubyで解いてみました。(^_^;

1,000,000までの正の整数の中に、1以上の数字からなる3つ以上のゾロ目が含まれる数は全部で何通りあるでしょうか?

 ちなみに、codepadやIdeone.comで計算させてみましたが、Time Outしてしまいました。(^_^;

●Zorome.rb

# Zorome.rb
tm=Time.now  # Timer Start
cnt=0
for x in 1..1000000 do
  if    x.to_s=~/111+/ then cnt+=1
  elsif x.to_s=~/222+/ then cnt+=1
  elsif x.to_s=~/333+/ then cnt+=1
  elsif x.to_s=~/444+/ then cnt+=1
  elsif x.to_s=~/555+/ then cnt+=1
  elsif x.to_s=~/666+/ then cnt+=1
  elsif x.to_s=~/777+/ then cnt+=1
  elsif x.to_s=~/888+/ then cnt+=1
  elsif x.to_s=~/999+/ then cnt+=1
  end
end
puts cnt
tm=Time.now-tm  # Timer Stop
print "Runtime : %.3f [sec]\n"%tm

●実行結果

33228
Runtime : 9.017 [sec]

※参考URL
http://web2.megaview.jp/details.php?&v=104204&vs=0&t=37179652&ts=80&l=132

たのしいRuby 第3版

たのしいRuby 第3版

Ruby逆引きハンドブック

Ruby逆引きハンドブック