Project Euler #13 Python. Incorrect carry over
<p>This problem asks to sum up 100 numbers, each 50 digits long. <a href="http://code.jasonbhill.com/python/project-euler-problem-13/" rel="nofollow">http://code.jasonbhill.com/python/project-euler-problem-13/</a></p>

<p>We can replace <code>\n</code> with "\n+" in Notepad++ yielding</p>

<pre><code>a=37107287533902102798797998220837590246510135740250
+46376937677490009712648124896970078050417018260538
...
+20849603980134001723930671666823555245252804609722
+53503534226472524250874054075591789781264330331690
print(a)
</code></pre>

<p><code>&gt;&gt;37107287533902102798797998220837590246510135740250</code> (incorrect)</p>

<p>We can as well replace <code>\n</code> with <code>\na+=</code> yielding </p>

<pre><code>a=37107287533902102798797998220837590246510135740250
a+=46376937677490009712648124896970078050417018260538
...
a+=20849603980134001723930671666823555245252804609722
a+=53503534226472524250874054075591789781264330331690
print(a)
</code></pre>

<p><code>&gt;&gt;553...</code> (correct)</p>

<p>This seems to be a feature of BigInteger arithmetic. Under which conditions a sum of all numbers (Method 1) yields different result from an iterative increment (Method 2)?</p>

<p>As you can see in the result,  the first set of instruction is not computing the sum. It preserved the first assignment. Since <code>+N</code> is on its own a valid instruction, the next lines after the assignment do nothing. Thus</p>

<pre><code>a=42
+1
print a
</code></pre>

<p>prints <code>42</code></p>

<p>To write an instruction over two lines, you need to escape the ending newline <code>\n</code> :</p>

<pre><code>a=42\
+1
</code></pre>

<p>43</p>

python