2018年10月15日月曜日

181015(3)

Ruby


A320466, A320470, A240026 とA240027

n が50以下のとき、出力してみた。

@ary1 = [1]
@ary2 = [1]
@ary3 = [1]
@ary4 = [1]

def partition(n, min, max)
  return [[]] if n == 0
  [max, n].min.downto(min).flat_map{|i| partition(n - i, min, i).map{|rest| [i, *rest]}}
end

# n > 0
def f(n)
  cnt1, cnt2, cnt3, cnt4 = 0, 0, 0, 0
  partition(n, 1, n).each{|ary|
    ary0 = (1..ary.size - 1).map{|i| ary[i - 1] - ary[i]}
    ary1 = ary0.sort
    if ary1 == ary0
      cnt1 += 1
      cnt2 += 1 if ary0.uniq == ary0
    end
    if ary1 == ary0.reverse
      cnt3 += 1
      cnt4 += 1 if ary0.uniq == ary0
    end
  }
  @ary1 << cnt1
  @ary2 << cnt2
  @ary3 << cnt3
  @ary4 << cnt4
end

n = 50
(1..n).each{|i| f(i)}
puts "A320466"
p @ary1
puts "A320470"
p @ary2
puts "A240026"
p @ary3
puts "A240027"
p @ary4

出力結果
A320466
[1, 1, 2, 3, 4, 5, 7, 7, 9, 12, 12, 13, 18, 17, 21, 25, 24, 27, 34, 33, 38, 44, 43, 47, 58, 56, 62, 70, 70, 78, 90, 84, 96, 109, 108, 118, 132, 127, 140, 158, 158, 167, 189, 185, 204, 221, 218, 236, 260, 261, 282]
A320470
[1, 1, 2, 2, 3, 4, 4, 5, 7, 6, 8, 10, 10, 11, 14, 13, 16, 19, 18, 20, 25, 23, 27, 31, 30, 34, 39, 37, 42, 48, 47, 50, 59, 56, 63, 70, 68, 74, 83, 82, 89, 97, 97, 104, 116, 113, 123, 133, 133, 142, 155]
A240026
[1, 1, 2, 3, 5, 6, 10, 12, 16, 21, 27, 32, 43, 50, 60, 75, 90, 103, 128, 146, 170, 203, 234, 264, 315, 355, 402, 467, 530, 589, 684, 764, 851, 969, 1083, 1195, 1360, 1504, 1659, 1863, 2063, 2258, 2531, 2779, 3039, 3379, 3709, 4032, 4474, 4880, 5304]
A240027
[1, 1, 2, 2, 4, 4, 5, 7, 9, 9, 13, 14, 16, 20, 23, 25, 32, 34, 38, 45, 51, 55, 65, 70, 77, 89, 99, 106, 122, 131, 143, 161, 177, 189, 211, 229, 248, 272, 298, 317, 349, 378, 406, 440, 479, 511, 554, 597, 640, 686, 744]

0 件のコメント:

コメントを投稿

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