2017年8月7日月曜日

170807

Ruby


Matiyasevich's Identity

ベルヌーイ数に関する恒等式を確認してみた。
三つ目が1997年にMatiyasevich が見つけた恒等式です。

def bernoulli(n)
  ary = []
  a = []
  (0..n).each{|i|
    a << 1r / (i + 1)
    i.downto(1){|j| a[j - 1] = j * (a[j - 1] - a[j])}
    ary << a[0] # Bn = a[0]
  }
  ary
end

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

n = 20
p b_ary = bernoulli(2 * n)
p ''
(2..n).each{|i|
  x = i + 1
  p [i, x == (0..i).inject(0){|s, j| s + ncr(i + 1, j) * b_ary[j]}, x]
}
p ''
(2..n).each{|i|
  x = -(i + 1) * b_ary[i]
  p [i, x == (2..i - 2).inject(0){|s, j| s + ncr(i, j) * b_ary[j] * b_ary[i - j]}, x]
}
p ''
(2..n).each{|i|
  x = i * (i + 1) * b_ary[i]
  p [i, x == (i + 2) * (2..i - 2).inject(0){|s, j| s + b_ary[j] * b_ary[i - j]} - 2 * (2..i - 2).inject(0){|s, j| s += ncr(i + 2, j) * b_ary[j] * b_ary[i - j]}, x]
}

出力結果
[(1/1), (1/2), (1/6), (0/1), (-1/30), (0/1), (1/42), (0/1), (-1/30), (0/1), (5/66), (0/1), (-691/2730), (0/1), (7/6), (0/1), (-3617/510), (0/1), (43867/798), (0/1), (-174611/330), (0/1), (854513/138), (0/1), (-236364091/2730), (0/1), (8553103/6), (0/1), (-23749461029/870), (0/1), (8615841276005/14322), (0/1), (-7709321041217/510), (0/1), (2577687858367/6), (0/1), (-26315271553053477373/1919190), (0/1), (2929993913841559/6), (0/1), (-261082718496449122051/13530)]
""
[2, true, 3]
[3, true, 4]
[4, true, 5]
[5, true, 6]
[6, true, 7]
[7, true, 8]
[8, true, 9]
[9, true, 10]
[10, true, 11]
[11, true, 12]
[12, true, 13]
[13, true, 14]
[14, true, 15]
[15, true, 16]
[16, true, 17]
[17, true, 18]
[18, true, 19]
[19, true, 20]
[20, true, 21]
""
[2, false, (-1/2)]
[3, true, (0/1)]
[4, true, (1/6)]
[5, true, (0/1)]
[6, true, (-1/6)]
[7, true, (0/1)]
[8, true, (3/10)]
[9, true, (0/1)]
[10, true, (-5/6)]
[11, true, (0/1)]
[12, true, (691/210)]
[13, true, (0/1)]
[14, true, (-35/2)]
[15, true, (0/1)]
[16, true, (3617/30)]
[17, true, (0/1)]
[18, true, (-43867/42)]
[19, true, (0/1)]
[20, true, (1222277/110)]
""
[2, false, (1/1)]
[3, true, (0/1)]
[4, true, (-2/3)]
[5, true, (0/1)]
[6, true, (1/1)]
[7, true, (0/1)]
[8, true, (-12/5)]
[9, true, (0/1)]
[10, true, (25/3)]
[11, true, (0/1)]
[12, true, (-1382/35)]
[13, true, (0/1)]
[14, true, (245/1)]
[15, true, (0/1)]
[16, true, (-28936/15)]
[17, true, (0/1)]
[18, true, (131601/7)]
[19, true, (0/1)]
[20, true, (-2444554/11)]

0 件のコメント:

コメントを投稿

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