2016年9月22日木曜日

160922

Ruby


Frieze Pattern

A007754 にならって、作ってみた。

def A(k, n)
  p [a = Array.new(n + 1, 1), a.size]
  return if k == 0
  p [b = (1..n).to_a, b.size]
  return if k == 1
  2.upto(k){|i|
    c = (0..n - i).map{|j| (b[j] * b[j + 1] - 1) / a[j + 1]}
    a, b = b, c
    p [b, b.size]
  }
end

n = 15
A(4, n)
p ''
A(n, n)

出力結果
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 16]
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 15]
[[1, 5, 11, 19, 29, 41, 55, 71, 89, 109, 131, 155, 181, 209], 14]
[[2, 18, 52, 110, 198, 322, 488, 702, 970, 1298, 1692, 2158, 2702], 13]
[[7, 85, 301, 751, 1555, 2857, 4825, 7651, 11551, 16765, 23557, 32215], 12]
""
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 16]
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 15]
[[1, 5, 11, 19, 29, 41, 55, 71, 89, 109, 131, 155, 181, 209], 14]
[[2, 18, 52, 110, 198, 322, 488, 702, 970, 1298, 1692, 2158, 2702], 13]
[[7, 85, 301, 751, 1555, 2857, 4825, 7651, 11551, 16765, 23557, 32215], 12]
[[33, 492, 2055, 5898, 13797, 28248, 52587, 91110, 149193, 233412, 351663], 11]
[[191, 3359, 16139, 52331, 136415, 307871, 626219, 1176779, 2077151, 3484415], 10]
[[1304, 26380, 143196, 517412, 1486768, 3666204, 8088260, 16383796, 31008072], 9]
[[10241, 234061, 1415821, 5639201, 17704801, 47352781, 112609421, 244580161], 8]
[[90865, 2314230, 15430835, 67153000, 228675645, 659272730, 1681053055], 7]
[[898409, 25222469, 183754199, 867349799, 3183754229, 9841738169], 6]
[[9791634, 300355398, 2373373752, 12075744186, 47527637790], 5]
[[116601199, 3879397705, 33043478329, 180268812991], 4]
[[1506023953, 54011212472, 493278801183], 3]
[[20967734143, 806288789375], 2]
[[313009988192], 1]

0 件のコメント:

コメントを投稿