Sagot :
Réponse :
Bjr,
Explications :
def p(n):
k=0
while 2**k<=n:
k+=1
if n-2**(k-1)<2**k-n:
return 2**(k-1)
else:
return 2**k
>>> p(532)
512
>>> p(1000)
1024
def p(n):
R=n
L=[]
M=""
while R!=0:
k=0
while 2**k<=R:
k+=1
L+=[2**(k-1)]
R=R-2**(k-1)
for i in range(len(L)):
if i==0:
M+=str(L[0])
else:
M+=" + "+str(L[i])
print(n,"=",M)
>>> p(8)
8 = 8
>>> p(67)
67 = 64 + 2 + 1
>>> p(1001)
1001 = 512 + 256 + 128 + 64 + 32 + 8 + 1
>>> p(131071)
131071 = 65536 + 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
>>>