2015年10月4日日曜日

151004(2)

Ruby


Number of permutations of the multiset {1,1,2,2,....,n,n} with no two consecutive terms equal(2)

n が大きくなると、次のコードの方が断然速い。

def A114938(n)
  i = 2
  a, b = 0, 2
  ary = [a, b]
  while i < n
    i += 1
    a, b = b, i * (2 * i - 1) * b + i * (i - 1) * a
    ary << b
  end
  ary
end
ary = A114938(16)

# OEIS A114938のデータ
ary0 =
[0,2,30,864,39480,2631600,241133760,29083420800,
 4467125013120,851371260364800,197158144895712000,
 54528028997584665600,17752366094818747392000,
 6720318485119046923315200,
 2927066537906697348594432000,
 1453437879238150456164433920000]
# 一致の確認
p ary == ary0

0 件のコメント:

コメントを投稿

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