単純なメモ化ならhashを使ってもいいのかもしれない
以下のようなハッシュを作ることで、pascalの3角形の値が計算できる。
pascal = Hash.new do |h,n| if n <= 1 h[n] = [1] else h[n] = [0,*h[n-1]].zip([h[n-1],0].flatten).map{ |x,y| x + y} end end #require 'benchmark' #Benchmark.bmbm do |x| # n = 100 # x.report("hash"){n.times{ |i| pascal[i]} } #end # >> Rehearsal ---------------------------------------- # >> hash 0.020000 0.000000 0.020000 ( 0.016962) # >> ------------------------------- total: 0.020000sec # >> # >> user system total real # >> hash 0.000000 0.000000 0.000000 ( 0.000161)