2016年5月24日火曜日

160524(3)

Ruby


(number of divisors of n of form 4m + 1) - (number of divisors of form 4m + 3)

オンライン整数列大辞典の
A001826(http://oeis.org/A001826/list)、
A001842(http://oeis.org/A001842/list)、
A002654(http://oeis.org/A002654/list)
と比較し、答え合わせしてみる。

def r1(n)
  (1..n).select{|i| n % i == 0 && i % 4 == 1}.size
end

def r3(n)
  (1..n).select{|i| n % i == 0 && i % 4 == 3}.size
end

def A001826(n)
  (1..n).map{|i| r1(i)}
end

def A001842(n)
  # 0から始まる
  (0..n).map{|i| r3(i)}
end

def A002654(n)
  a, b = A001826(n), A001842(n)[1..-1]
  (0..n - 1).map{|i| a[i] - b[i]}
end

ary = A001826(105)
# OEIS A001826のデータ
ary0 =
[1,1,1,1,2,1,1,1,2,2,1,1,2,1,2,1,2,2,1,2,2,1,1,1,
 3,2,2,1,2,2,1,1,2,2,2,2,2,1,2,2,2,2,1,1,4,1,1,1,2,
 3,2,2,2,2,2,1,2,2,1,2,2,1,3,1,4,2,1,2,2,2,1,2,2,2,
 3,1,2,2,1,2,3,2,1,2,4,1,2,1,2,4,2,1,2,1,2,1,2,2,3,
 3,2,2,1,2,4]
# 一致の確認
p ary == ary0

ary = A001842(86)
# OEIS A001842のデータ
ary0 =
[0,0,0,1,0,0,1,1,0,1,0,1,1,0,1,2,0,0,1,1,0,2,1,1,
 1,0,0,2,1,0,2,1,0,2,0,2,1,0,1,2,0,0,2,1,1,2,1,1,1,
 1,0,2,0,0,2,2,1,2,0,1,2,0,1,3,0,0,2,1,0,2,2,1,1,0,
 0,3,1,2,2,1,0,2,0,1,2,0,1]
# 一致の確認
p ary == ary0

ary = A002654(105)
# OEIS A002654のデータ
ary0 =
[1,1,0,1,2,0,0,1,1,2,0,0,2,0,0,1,2,1,0,2,0,0,0,0,
 3,2,0,0,2,0,0,1,0,2,0,1,2,0,0,2,2,0,0,0,2,0,0,0,1,
 3,0,2,2,0,0,0,0,2,0,0,2,0,0,1,4,0,0,2,0,0,0,1,2,2,
 0,0,0,0,0,2,1,2,0,0,4,0,0,0,2,2,0,0,0,0,0,0,2,1,0,
 3,2,0,0,2,0]
# 一致の確認
p ary == ary0

出力結果
true
true
true

0 件のコメント:

コメントを投稿

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