2018年7月7日土曜日

180707

Ruby


A316384(1)

谷に三角形を積み上げていくことを考える。
(ただし、上の部分以外は、ピラミッドのように隙間なく埋めていくものとする。)
このようにn 個積み上げたもののうち、左右対称なものを数えてみた。

def f(ary)
  f_ary = []
  while !(ary.all?{|i| i == 0})
    a = []
    s = 0
    ary.each{|i|
      if i == 1
        a << 0
        s += 1
      elsif i > 0
        a << i - 2
        s += 2
      end
    }
    ary = a
    f_ary << s
  end
  f_ary
end

def A316384(n)
  return 1 if n < 3
  a = (1..n - 1).map{|i| [i]}
  ary = []
  while a.size != 0
    b = []
    a.each{|i|
      x = i[-1].to_i
      s = n - i.inject(:+)
      t = x % 2
      (x + t..s - 1).each{|j| b << i.clone + [j]}
      ary << i.clone + [s] if s >= x + t
    }
    a = b
  end
  ary.select{|i| i.reverse == f(i)}.size
end

p (0..90).map{|i| A316384(i)}

出力結果
[1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 4, 2, 5, 2, 5, 2, 6, 3, 8, 4, 9, 4, 10, 4, 12, 6, 15, 7, 17, 7, 19, 8, 22, 10, 26, 12, 30, 13, 33, 14, 38, 17, 45, 21, 51, 22, 56, 24, 64, 29, 74, 33, 83, 36, 92, 40, 104, 46, 119, 53, 133, 58, 147, 63, 165, 73, 187, 83, 208, 90, 229, 99, 256, 113, 288, 127, 319, 138, 351, 152, 390, 171, 435, 191, 481]

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。