2019年3月11日月曜日

190311

Ruby


A306646

Lucas数列, Perrin数列, ... , を出力してみた。

def A(k, n)
  a = [k + 1] + Array.new(k - 1, 0) + [k]
  ary = [k + 1]
  while ary.size < n + 1
    a = *a[1..-1], a[0] + a[1]
    ary << a[0]
  end
  ary
end

n = 30
(1..10).each{|i| p [i, A(i, n)]}

出力結果
[1, [2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843, 1364, 2207, 3571, 5778, 9349, 15127, 24476, 39603, 64079, 103682, 167761, 271443, 439204, 710647, 1149851, 1860498]]
[2, [3, 0, 2, 3, 2, 5, 5, 7, 10, 12, 17, 22, 29, 39, 51, 68, 90, 119, 158, 209, 277, 367, 486, 644, 853, 1130, 1497, 1983, 2627, 3480, 4610]]
[3, [4, 0, 0, 3, 4, 0, 3, 7, 4, 3, 10, 11, 7, 13, 21, 18, 20, 34, 39, 38, 54, 73, 77, 92, 127, 150, 169, 219, 277, 319, 388]]
[4, [5, 0, 0, 0, 4, 5, 0, 0, 4, 9, 5, 0, 4, 13, 14, 5, 4, 17, 27, 19, 9, 21, 44, 46, 28, 30, 65, 90, 74, 58, 95]]
[5, [6, 0, 0, 0, 0, 5, 6, 0, 0, 0, 5, 11, 6, 0, 0, 5, 16, 17, 6, 0, 5, 21, 33, 23, 6, 5, 26, 54, 56, 29, 11]]
[6, [7, 0, 0, 0, 0, 0, 6, 7, 0, 0, 0, 0, 6, 13, 7, 0, 0, 0, 6, 19, 20, 7, 0, 0, 6, 25, 39, 27, 7, 0, 6]]
[7, [8, 0, 0, 0, 0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 7, 15, 8, 0, 0, 0, 0, 7, 22, 23, 8, 0, 0, 0, 7, 29, 45]]
[8, [9, 0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 8, 17, 9, 0, 0, 0, 0, 0, 8, 25, 26, 9, 0, 0, 0]]
[9, [10, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 0, 0, 0, 9, 19, 10, 0, 0, 0, 0, 0, 0, 9, 28, 29, 10]]
[10, [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 10, 21, 11, 0, 0, 0, 0, 0, 0, 0, 10]]

0 件のコメント:

コメントを投稿

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