2023年2月5日日曜日

230205

Ruby


べき乗和の公式

出力してみた。

def ncr(n, r)
  return 1 if r == 0
  (n - r + 1..n).inject(:*) / (1..r).inject(:*)
end

# f(x+1) - f(x) = (aryが表す多項式) を満たすf(x)を求める
def A(ary)
  a = []
  ary.size.downto(1){|i|
    b = ary[0] / i.to_r
    a << b
    (1..i).each{|j| ary[j - 1] -= b * ncr(i, j)}
    ary = ary[1..-1]
  }
  a
end

def B(n)
  # f(x+1) - f(x) = (1+x)^n
  (0..n).each{|i| p A((0..i).map{|j| ncr(i, j)})}
end

B(10)

出力結果
[(1/1)]
[(1/2), (1/2)]
[(1/3), (1/2), (1/6)]
[(1/4), (1/2), (1/4), (0/1)]
[(1/5), (1/2), (1/3), (0/1), (-1/30)]
[(1/6), (1/2), (5/12), (0/1), (-1/12), (0/1)]
[(1/7), (1/2), (1/2), (0/1), (-1/6), (0/1), (1/42)]
[(1/8), (1/2), (7/12), (0/1), (-7/24), (0/1), (1/12), (0/1)]
[(1/9), (1/2), (2/3), (0/1), (-7/15), (0/1), (2/9), (0/1), (-1/30)]
[(1/10), (1/2), (3/4), (0/1), (-7/10), (0/1), (1/2), (0/1), (-3/20), (0/1)]
[(1/11), (1/2), (5/6), (0/1), (-1/1), (0/1), (1/1), (0/1), (-1/2), (0/1), (5/66)]

0 件のコメント:

コメントを投稿

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