Ducci sequence
[1, 2, … , n]からスタートしたとき、周期の長さを
出力するコードを書いてみた。
def d(ary)
d_ary = []
ary1 = ary.clone
(ary1 << ary[0]).each_cons(2){|a| d_ary << a.max - a.min}
d_ary
end
(1..24).each{|n|
ary = (1..n).to_a
i = 0
s_ary = [ary]
d_ary = d(ary)
i += 1
while !s_ary.include?(d_ary)
ary = d_ary
s_ary << ary
d_ary = d(ary)
i += 1
end
ary = d_ary
p [n, i - s_ary.index(ary)]
}
出力結果
[1, 1]
[2, 1]
[3, 3]
[4, 1]
[5, 15]
[6, 6]
[7, 7]
[8, 1]
[9, 63]
[10, 30]
[11, 341]
[12, 12]
[13, 819]
[14, 14]
[15, 15]
[16, 1]
[17, 255]
[18, 126]
[19, 9709]
[20, 60]
[21, 63]
[22, 682]
[23, 2047]
[24, 24]
[2, 1]
[3, 3]
[4, 1]
[5, 15]
[6, 6]
[7, 7]
[8, 1]
[9, 63]
[10, 30]
[11, 341]
[12, 12]
[13, 819]
[14, 14]
[15, 15]
[16, 1]
[17, 255]
[18, 126]
[19, 9709]
[20, 60]
[21, 63]
[22, 682]
[23, 2047]
[24, 24]
0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。