2023年4月30日日曜日

230430

Ruby


A275216

q-binomialの係数を出力してみた。

def plus(f_ary, b_ary)
  s1, s2 = f_ary.size, b_ary.size
  ary = Array.new([s1, s2].max, 0)
  (0..s1 - 1).each{|i| ary[i] += f_ary[i]}
  (0..s2 - 1).each{|i| ary[i] += b_ary[i]}
  ary
end

def A(n)
  ary = [[1]]
  a = [[[1]]]
  (1..n).each{|i|
    new_ary = [[0]] + ary
    (0..i - 1).each{|j| 
      new_ary[j] = plus(new_ary[j], [0] * j + ary[j])
    }
    ary = new_ary
    a << ary
  }
  a
end

n = 10
A(n).each_with_index{|i, num|
  puts "Row #{num}"
  i.each{|j| p j}
}

出力結果
Row 0
[1]
Row 1
[1]
[1]
Row 2
[1]
[1, 1]
[1]
Row 3
[1]
[1, 1, 1]
[1, 1, 1]
[1]
Row 4
[1]
[1, 1, 1, 1]
[1, 1, 2, 1, 1]
[1, 1, 1, 1]
[1]
Row 5
[1]
[1, 1, 1, 1, 1]
[1, 1, 2, 2, 2, 1, 1]
[1, 1, 2, 2, 2, 1, 1]
[1, 1, 1, 1, 1]
[1]
Row 6
[1]
[1, 1, 1, 1, 1, 1]
[1, 1, 2, 2, 3, 2, 2, 1, 1]
[1, 1, 2, 3, 3, 3, 3, 2, 1, 1]
[1, 1, 2, 2, 3, 2, 2, 1, 1]
[1, 1, 1, 1, 1, 1]
[1]
Row 7
[1]
[1, 1, 1, 1, 1, 1, 1]
[1, 1, 2, 2, 3, 3, 3, 2, 2, 1, 1]
[1, 1, 2, 3, 4, 4, 5, 4, 4, 3, 2, 1, 1]
[1, 1, 2, 3, 4, 4, 5, 4, 4, 3, 2, 1, 1]
[1, 1, 2, 2, 3, 3, 3, 2, 2, 1, 1]
[1, 1, 1, 1, 1, 1, 1]
[1]
Row 8
[1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 2, 2, 3, 3, 4, 3, 3, 2, 2, 1, 1]
[1, 1, 2, 3, 4, 5, 6, 6, 6, 6, 5, 4, 3, 2, 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, 3, 2, 1, 1]
[1, 1, 2, 2, 3, 3, 4, 4, 5, 4, 4, 3, 3, 2, 2, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1]

0 件のコメント:

コメントを投稿

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