知恵袋の数学の質問のゾロ目の問題を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
- 作者: 高橋征義,後藤裕蔵,まつもとゆきひろ
- 出版社/メーカー: ソフトバンククリエイティブ
- 発売日: 2010/03/31
- メディア: 単行本
- 購入: 15人 クリック: 394回
- この商品を含むブログ (79件) を見る
- 作者: るびきち
- 出版社/メーカー: シーアンドアール研究所
- 発売日: 2009/05/25
- メディア: 単行本
- 購入: 24人 クリック: 263回
- この商品を含むブログ (72件) を見る