+ 7
a = 2 for i in range(-5,5): a=a+a print(a)
Why the answer to this code is 2048
5 odpowiedzi
+ 4
Hi! Becouse from -5 to 4:
-5 2+2
-4 4+4
-3 8+8
-2 16+16
-1 32+32
0 64+64
1 128+128
2 256 +256
3 512+512
4 1024+1024
+ 3
a = 2
loop:
i = -5, a = 2 + 2 = 4
i = -4, a = 4 + 4 = 8
i = -3, a = 8 + 8 = 16
i = -2, a = 16 + 16 = 32
i = -1, a = 32 + 32 = 64
i = 0, a = 64 + 64 = 128
i = 1, a = 128 + 128 = 256
i = 2, a = 256 + 256 = 512
i = 3, a = 512 + 512 = 1024
i = 4, a = 1024 + 1024 = 2048
end loop (as range doesn't include upper bound)
+ 3
range(-5,5):
Difference bt -5 and 5 is 10 => 10 loops
each loop multiplies by 2
2**1 * 2**10= 2**11 = 2048
0
a = 2
loop:
i = -5, a = 2 + 2 = 4
i = -4, a = 4 + 4 = 8
i = -3, a = 8 + 8 = 16
i = -2, a = 16 + 16 = 32
i = -1, a = 32 + 32 = 64
i = 0, a = 64 + 64 = 128
i = 1, a = 128 + 128 = 256
i = 2, a = 256 + 256 = 512
i = 3, a = 512 + 512 = 1024
i = 4, a = 1024 + 1024 = 2048
end loop (as range doesn't include upper bound)
0
a = 2
for i in range(-5,5):
a=a+a
print(a)
Why the answer to this code is 2048