2020年10月20日火曜日

201020(2)

Crystal


A006950(2)

先程と同様にCrystal に書き換えてみた。

def a006950(n)
  return 1 if n < 3
  a = (1..n - 1).map{|i| [i]}
  ary = [[n]]
  while a.size != 0
    b = Array(Array(Int32)).new
    a.each{|i|
      x = i[-1].to_i
      s = n - i.sum
      t = x % 2
      (x + t..s - 1).each{|j| b << i.clone + [j]}
      ary << i.clone + [s] if s >= x + t
    }
    a = b
  end
  ary.size
end

p (0..90).map{|i| a006950(i)}

出力結果
[1, 1, 1, 2, 3, 4, 5, 7, 10, 13, 16, 21, 28, 35, 43, 55, 70, 86, 105, 130, 161, 196, 236, 287, 350, 420, 501, 602, 722, 858, 1016, 1206, 1431, 1687, 1981, 2331, 2741, 3206, 3740, 4368, 5096, 5922, 6868, 7967, 9233, 10670, 12306, 14193, 16357, 18803, 21581, 24766, 28394, 32490, 37128, 42411, 48406, 55159, 62777, 71416, 81182, 92152, 104494, 118434, 134131, 151725, 171467, 193676, 218607, 246491, 277691, 312677, 351841, 395537, 444314, 498852, 559727, 627509, 703002, 787171, 880896, 985043, 1100775, 1229494, 1372499, 1531086, 1706958, 1902121, 2118469, 2357947, 2623017]

201020

Crystal


A316384(2)

Crystal ではinject ではなく、reduce だが、
和の場合はsum が使える。

def f(ary)
  f_ary = Array(Int32).new
  while !(ary.all?{|i| i == 0})
    a = Array(Int32).new
    s = 0
    ary.each{|i|
      if i == 1
        a << 0
        s += 1
      elsif i > 0
        a << i - 2
        s += 2
      end
    }
    ary = a
    f_ary << s
  end
  f_ary
end

def a316384(n)
  return 1 if n < 3
  a = (1..n - 1).map{|i| [i]}
  ary = Array(Array(Int32)).new
  while a.size != 0
    b = Array(Array(Int32)).new
    a.each{|i|
      x = i[-1].to_i
      s = n - i.sum
      t = x % 2
      (x + t..s - 1).each{|j| b << i.clone + [j]}
      ary << i.clone + [s] if s >= x + t
    }
    a = b
  end
  ary.select{|i| i.reverse == f(i)}.size
end

p (0..90).map{|i| a316384(i)}

出力結果
[1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 4, 2, 5, 2, 5, 2, 6, 3, 8, 4, 9, 4, 10, 4, 12, 6, 15, 7, 17, 7, 19, 8, 22, 10, 26, 12, 30, 13, 33, 14, 38, 17, 45, 21, 51, 22, 56, 24, 64, 29, 74, 33, 83, 36, 92, 40, 104, 46, 119, 53, 133, 58, 147, 63, 165, 73, 187, 83, 208, 90, 229, 99, 256, 113, 288, 127, 319, 138, 351, 152, 390, 171, 435, 191, 481]

2020年10月16日金曜日

201016

Crystal


Number of solutions to +- 1 +- 2 +- 3 +- ... +- n = 0(6)

以下を用いて、n が4000 のときの値を求めることはおすすめしない。

require "big"

def a063865(n)
  ary = [1.to_big_i]
  m = n * (n + 1) // 4
  a = Array.new(m + 1){0.to_big_i}
  a[0] = 1.to_big_i
  (1..n).each{|i|
    b = a.clone
    (0..[(i - 1) * i / 2, m - i].min).each{|k| b[k + i] += a[k]}
    a = b
    i * (i + 1) % 4 == 0 ? ary << a[i * (i + 1) // 4] : ary << 0.to_big_i
  }
  ary
end

p a063865(1000)[-1]

出力結果
467708586991053780130476928496471502490480200263913521598374850751872554493810444457551220080062611698129340084492921358002542871554381676735263500063986630523649271451391806356678763700553308953903873511563867155215734010997332808966175771715884247295296277348179194597363883854664431808932677416

2020年10月10日土曜日

201010

Python


足し算と引き算だけで円を描く(3)

Python で書いてみた。

def A(r):
    xy = [['□' for _ in range(r + 1)] for _ in range(r + 1)]
    a, x, y = r, 0, r
    while x <= y:
        xy[x][y], xy[y][x] = '■', '■'
        a, x = a - x - x - 1, x + 1
        if a < 0:
            a, y = a + y + y - 1, y - 1 
    for i in xy:
        print(''.join(i))

for i in range(21):
    A(i)

出力結果
□■
■■
□□■
□□■
■■□
□□□■
□□□■
□□■□
■■□□
□□□□■
□□□□■
□□□□■
□□□■□
■■■□□
□□□□□■
□□□□□■
□□□□□■
□□□□■□
□□□■□□
■■■□□□
□□□□□□■
□□□□□□■
□□□□□□■
□□□□□■□
□□□□□■□
□□□■■□□
■■■□□□□
□□□□□□□■
□□□□□□□■
□□□□□□□■
□□□□□□■□
□□□□□□■□
□□□□□■□□
□□□■■□□□
■■■□□□□□
□□□□□□□□■
□□□□□□□□■
□□□□□□□□■
□□□□□□□■□
□□□□□□□■□
□□□□□□■□□
□□□□□■■□□
□□□■■□□□□
■■■□□□□□□
□□□□□□□□□■
□□□□□□□□□■
□□□□□□□□□■
□□□□□□□□□■
□□□□□□□□■□
□□□□□□□□■□
□□□□□□□■□□
□□□□□□■□□□
□□□□■■□□□□
■■■■□□□□□□
□□□□□□□□□□■
□□□□□□□□□□■
□□□□□□□□□□■
□□□□□□□□□□■
□□□□□□□□□■□
□□□□□□□□□■□
□□□□□□□□■□□
□□□□□□□■□□□
□□□□□□■□□□□
□□□□■■□□□□□
■■■■□□□□□□□
□□□□□□□□□□□■
□□□□□□□□□□□■
□□□□□□□□□□□■
□□□□□□□□□□□■
□□□□□□□□□□■□
□□□□□□□□□□■□
□□□□□□□□□■□□
□□□□□□□□□■□□
□□□□□□□□■□□□
□□□□□□■■□□□□
□□□□■■□□□□□□
■■■■□□□□□□□□
□□□□□□□□□□□□■
□□□□□□□□□□□□■
□□□□□□□□□□□□■
□□□□□□□□□□□□■
□□□□□□□□□□□■□
□□□□□□□□□□□■□
□□□□□□□□□□■□□
□□□□□□□□□□■□□
□□□□□□□□□■□□□
□□□□□□□□■□□□□
□□□□□□■■□□□□□
□□□□■■□□□□□□□
■■■■□□□□□□□□□
□□□□□□□□□□□□□■
□□□□□□□□□□□□□■
□□□□□□□□□□□□□■
□□□□□□□□□□□□□■
□□□□□□□□□□□□■□
□□□□□□□□□□□□■□
□□□□□□□□□□□□■□
□□□□□□□□□□□■□□
□□□□□□□□□□■□□□
□□□□□□□□□□■□□□
□□□□□□□□■■□□□□
□□□□□□□■□□□□□□
□□□□■■■□□□□□□□
■■■■□□□□□□□□□□
□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□■
□□□□□□□□□□□□□■□
□□□□□□□□□□□□□■□
□□□□□□□□□□□□□■□
□□□□□□□□□□□□■□□
□□□□□□□□□□□□■□□
□□□□□□□□□□□■□□□
□□□□□□□□□□■□□□□
□□□□□□□□□■□□□□□
□□□□□□□■■□□□□□□
□□□□■■■□□□□□□□□
■■■■□□□□□□□□□□□
□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□■□□
□□□□□□□□□□□□■□□□
□□□□□□□□□□□■□□□□
□□□□□□□□□□■□□□□□
□□□□□□□□□■□□□□□□
□□□□□□□■■□□□□□□□
□□□□■■■□□□□□□□□□
■■■■□□□□□□□□□□□□
□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□■□□□□
□□□□□□□□□□□■□□□□□
□□□□□□□□□■■□□□□□□
□□□□□□□■■□□□□□□□□
□□□□□■■□□□□□□□□□□
■■■■■□□□□□□□□□□□□
□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□■□□□□
□□□□□□□□□□□□■□□□□□
□□□□□□□□□□□■□□□□□□
□□□□□□□□□□■□□□□□□□
□□□□□□□□■■□□□□□□□□
□□□□□■■■□□□□□□□□□□
■■■■■□□□□□□□□□□□□□
□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□□■□□□□
□□□□□□□□□□□□□□■□□□□
□□□□□□□□□□□□□■□□□□□
□□□□□□□□□□□■■□□□□□□
□□□□□□□□□□■□□□□□□□□
□□□□□□□□■■□□□□□□□□□
□□□□□■■■□□□□□□□□□□□
■■■■■□□□□□□□□□□□□□□
□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□□□■□□□□
□□□□□□□□□□□□□□■□□□□□
□□□□□□□□□□□□□■□□□□□□
□□□□□□□□□□□□■□□□□□□□
□□□□□□□□□□■■□□□□□□□□
□□□□□□□□■■□□□□□□□□□□
□□□□□■■■□□□□□□□□□□□□
■■■■■□□□□□□□□□□□□□□□
□□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□□■
□□□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□□□■□
□□□□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□□□□■□□
□□□□□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□□□□□■□□□
□□□□□□□□□□□□□□□□■□□□□
□□□□□□□□□□□□□□□■□□□□□
□□□□□□□□□□□□□□■□□□□□□
□□□□□□□□□□□□□■□□□□□□□
□□□□□□□□□□□□■□□□□□□□□
□□□□□□□□□□■■□□□□□□□□□
□□□□□□□□■■□□□□□□□□□□□
□□□□□■■■□□□□□□□□□□□□□
■■■■■□□□□□□□□□□□□□□□□

2020年10月9日金曜日

201009

Ruby


足し算と引き算だけで円を描く(2)

文字コードの設定が変わると出力がかわる。
chcp で932 の場合は
chcp 65001 で変更すればよい。