ワンライナーでつくってみた(rubyで魔方陣その3)
驚愕の事実が…工夫したときよりも速い。てかprologとスピード同じ…
$ time ruby -e '(1..9).to_a.permutation.select{|i|x=i[0]+i[1]+i[2];x==i[3]+i[4]+i[5]&&x==i[6]+i[7]+i[8]&&x==i[0]+i[3]+i[6]&&x==i[1]+i[4]+i[7]&&x==i[2]+i[5]+i[8]&&x==i[0]+i[4]+i[8]&&x==i[2]+i[4]+i[6]}.each{|i|p i}' [2, 7, 6, 9, 5, 1, 4, 3, 8] [2, 9, 4, 7, 5, 3, 6, 1, 8] [4, 3, 8, 9, 5, 1, 2, 7, 6] [4, 9, 2, 3, 5, 7, 8, 1, 6] [6, 1, 8, 7, 5, 3, 2, 9, 4] [6, 7, 2, 1, 5, 9, 8, 3, 4] [8, 1, 6, 3, 5, 7, 4, 9, 2] [8, 3, 4, 1, 5, 9, 6, 7, 2] real 0m0.469s user 0m0.452s sys 0m0.004s
なんだかなぁ。素直が一番って事?
もしかするとこれぐらいになるとファイルのオープンとかに時間がとられているのかな?
$ time ruby -e 'puts "hello,world"' hello,world real 0m0.016s user 0m0.008s sys 0m0.004s $ cat > hello.rb puts "hello,world" $ time ruby hello.rb hello,world real 0m0.016s user 0m0.008s sys 0m0.000s
全然違った。