平面的2進数(2)
1 を足していってn にするのではなく、直接n を求めてみた。
def f(n)
# lは大きめ
l = 100
a = Array.new(l){Array.new(l, 0)}
a[0][0] = n
flag = true
m, x = 0, 0
while flag
flag = false
(0..m).each{|i|
a[i][m - i], j = a[i][m - i] % 2, a[i][m - i] / 2
if j > 0
flag = true
x = [i + 1, x].max
a[i + 1][m - i] += j
a[i][m - i + 1] += j
end
}
m += 1
end
(0..x).each{|i| p a[x - i][0..x].reverse}
return ""
end
(0..37).each{|i|
p i
p f(i)
}
出力結果
0
[0]
""
1
[1]
""
2
[0, 1]
[1, 0]
""
3
[0, 1]
[1, 1]
""
4
[0, 1, 1]
[1, 0, 0]
[1, 0, 0]
""
5
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
""
6
[0, 1, 1]
[1, 0, 1]
[1, 1, 0]
""
7
[0, 1, 1]
[1, 0, 1]
[1, 1, 1]
""
8
[0, 1, 1, 1]
[1, 0, 1, 0]
[1, 1, 0, 0]
[1, 0, 0, 0]
""
9
[0, 1, 1, 1]
[1, 0, 1, 0]
[1, 1, 0, 0]
[1, 0, 0, 1]
""
10
[0, 1, 1, 1]
[1, 0, 1, 0]
[1, 1, 0, 1]
[1, 0, 1, 0]
""
11
[0, 1, 1, 1]
[1, 0, 1, 0]
[1, 1, 0, 1]
[1, 0, 1, 1]
""
12
[0, 1, 1, 1, 0]
[1, 0, 1, 0, 1]
[1, 1, 0, 0, 1]
[1, 0, 0, 0, 0]
[0, 1, 1, 0, 0]
""
13
[0, 1, 1, 1, 0]
[1, 0, 1, 0, 1]
[1, 1, 0, 0, 1]
[1, 0, 0, 0, 0]
[0, 1, 1, 0, 1]
""
14
[0, 1, 1, 1, 0]
[1, 0, 1, 0, 1]
[1, 1, 0, 0, 1]
[1, 0, 0, 0, 1]
[0, 1, 1, 1, 0]
""
15
[0, 1, 1, 1, 0]
[1, 0, 1, 0, 1]
[1, 1, 0, 0, 1]
[1, 0, 0, 0, 1]
[0, 1, 1, 1, 1]
""
16
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