# 通过重复添加2个数字来生成任何数字

14

• `x+=y`- `x`被替换`(x + y) mod 65536`; `y`不变
• `y+=x` -类似地 `y`
• `x+=x`- `x`被替换`2x mod 65536`; 仅在`x`偶数时合法
• `y+=y` -类似地 `y`

``````y+=x
x+=y
x+=y
x+=x
x+=x
x+=x
y+=x
``````

``````y+=x
x+=y
y+=x
x+=y
y+=x
x+=y
y+=x
``````

（这个难题的灵感来自于我最近必须生成的16位处理器的一些代码）

PS：我想知道-最佳程序最长的数字是多少？

9

Sp3000

anatolyg 2014年

anatolyg 2014年

3

bcsb1001 2015年

2

# CJam，31岁

``````qi{_4%!:X)/X!-'xX+"
y+="@}h;]W%`
``````

``````qi          "Read input and convert to integer.";
{           "Do...";
_4%!:X    "Assign (value mod 4 == 0) to X.";
)/X!-     "If X, divide value by 2. If not X, decrement value.";
'xX+      "If X, put 'y' on the stack. If not X, put 'x' on the stack.";
"
y+="        "Put '\ny+=' on the stack.";
@         "Rotate top 3 elements of stack left so the value is on top.";
}h          "... while value is not zero.";
;           "Discard zero value on stack.";
]W%         "Collect stack into array and reverse.";
``````

CChak

8

# 佩尔107 97

``````sub h{(\$i)=@_;return if((\$i%=65536)==0);(\$i%4==0)?do{h(\$i/2);say"y+=y";}:do{h(\$i-1);say"y+=x";}}
``````

``````sub h{                           # Declare the subroutine, it should be called referencing an integer value
(\$i)=@_;                      # Assign the i variable to the integer used in the call
return if((\$i%=65536)==0);    # Check for base condition of called by 0, and enforce modulo from the start.
(\$i%4==0) ?                   # If the value passed is even, and will result in an even number if we halve it
do{h(\$i/2);say"y+=y";}        # Then do so and recurse
:do{h(\$i-1);say"y+=x";}       # Otherwise "subtract one" and recurse
}                                # Note that the print statements get called in reverse order as we exit.
``````

``````sub h{(\$i)=@_;return if((\$i%=65536)==0);(\$i%2==0)?do{h(\$i/2);say"y+=y";}:do{h(\$i-1);say"y+=x";}}
``````

anatolyg 2014年

7

# Python 3，202个字节

``````def S(n):
q=[(1,0,"")];k=65536
while q:
x,y,z=q.pop(0)
if n in{x,y}:print(z);return
q+=[((x+y)%k,y,z+"x+=y\n"),(x,(x+y)%k,z+"y+=x\n")]+[(2*x%k,y,z+"x+=x\n")]*(~x&1)+[(x,2*y%k,z+"y+=y\n")]*(~y&1)``````

（感谢@rationalis几个字节）

``````y+=x
x+=y
x+=x
x+=x
x+=x
x+=x
y+=x
y+=x
x+=y
``````

5

# Dyalog APL，49个字符/字节*

``````{0=N←⍵|⍨2*16:⍬⋄0=4|N:⎕←'y+=y'⊣∇N÷2⋄⎕←'y+=x'⊣∇N-1}
``````

``````    {0=N←⍵|⍨2*16:⍬⋄0=4|N:⎕←'y+=y'⊣∇N÷2⋄⎕←'y+=x'⊣∇N-1} 25
y+=x
y+=x
y+=y
y+=x
y+=x
y+=y
y+=y
y+=x
``````

``````{
N←(2*16)|⍵                 # assign the argument modulo 65536 to N
0=N: ⍬                     # if N = 0, return an empty value (that's a Zilde, not a 0)
0=4|N: ⎕←'y+=y' ⊣ ∇N÷2     # if N mod 4 = 0, recurse with N÷2 and *then* print 'y+=y'
⎕←'y+=x' ⊣ ∇N-1            # otherwise, recurse with N-1 and *then* print 'y+=x'
}
``````

* Dyalog APL支持传统字符集，该字符集具有映射到高128字节值的APL符号。因此，仅使用ASCII字符和APL符号的APL程序可以视为字节==字符。

3

# Python，183

``````def S(n):
b,c,e=16,'x+=x\n','x+=y\n';s=d='y+=x\n';a=i=0
if n<2:return
while~n&1:n>>=1;a+=1
while n:n>>=1;s+=[e,c][i]+d*(n&1);i=1;b-=1
while a:s+=[c,c*b+e*2][i];i=0;a-=1
print(s)``````

### 测试中

``````# Do an exhaustive breadth-first search to find the shortest program for
# each valid input
def bfs():
d = {(0,1):0}
k = 0xFFFF
s = set(range(k+1))
current = [(0,1)]
nexts = []
if pt in d: return
d[pt] = dist
s.difference_update(pt)
n.append(pt)
i = 0
while len(s) > 0:
i += 1
for p in current:
x,y = p
if y%2 == 0: add(tuple(sorted((x,y+y&k))), i, nexts)
if x%2 == 0: add(tuple(sorted((x+x&k,y))), i, nexts)
current = nexts
nexts = []
print(len(d),len(s))

# Mine (@rationalis)
def S(n):
b,c,e=16,'x+=x\n','x+=y\n';s=d='y+=x\n';a=i=0
if n<2:return ''
while~n&1:n>>=1;a+=1
while n:n>>=1;s+=[e,c][i]+d*(n&1);i=1;b-=1
while a:s+=[c,c*b+e*2][i];i=0;a-=1
return s

# @CChak's approach
def U(i):
if i<1:return ''
return U(i//2)+'y+=y\n' if i%4==0 else U(i-1)+'y+=x\n'

# Use mine on odd numbers and @CChak's on even numbers
def V(i):
return S(i) if i % 2 == 1 else U(i)

# Simulate a program in the hypothetical machine language
def T(s):
x,y = 1,0
for l in s.split():
if l == 'x+=x':
if x % 2 == 1: return 1,0
x += x
elif l == 'y+=y':
if y % 2 == 1: return 1,0
y += y
elif l == 'x+=y': x += y
elif l == 'y+=x': y += x
x %= 1<<16
y %= 1<<16
return x,y

# Test a solution on all values 0 to 65535 inclusive
# Max op limit only for my own solution
def test(f):
max_ops = 33 if f==S else 1000
for i in range(1<<16):
s = f(i); t = T(s)
if i not in t or len(s)//5 > max_ops:
print(s,i,t)
break

# Compare two solutions
def test2(f,g):
lf = [len(f(i)) for i in range(2,1<<16)]
lg = [len(g(i)) for i in range(2,1<<16)]
l = [lf[i]/lg[i] for i in range(len(lf))]
print(sum(l)/len(l))
print(sum(lf)/sum(lg))

# Test by default if script is executed
def main():
test()

if __name__ == '__main__':
main()``````

1
201字节中有很多逻辑！
anatolyg'1

@analtolyg我能说什么，我喜欢数学并且有点摆弄。我可能会研究其他方法，因为偶数解决方案还有改进的余地。

Sp3000