Sum of even integers from a to b in Python

This is my code:

def sum_even(a, b): count = 0 for i in range(a, b, 1): if(i % 2 == 0): count += [i] return count 

As an example, I cited print (sum_even (3,7)), and the result is 0. I cannot understand what is wrong.

+4
source share
10 answers

Your indentation is off, it should be:

 def sum_even(a, b): count = 0 for i in range(a, b, 1): if(i % 2 == 0): count += i return count 

so that return count does not fall into your for loop (in this case, it returns to the 1st iteration, forcing it to return 0)

(And change [i] to i )


NOTE: another problem - you have to be careful when using range :

 >>> range(3,7) [3, 4, 5, 6] 

so if you need to call:

  • sum_even(3,7)
  • sum_even(3,8)

right now they both print 10 , which is not true for the sum of even integers from 3 to 8 inclusive.

What you really want is probably instead:

 def sum_even(a, b): return sum(i for i in range(a, b + 1) if i % 2 == 0) 
+9
source
  • Move the return from the scope of the for loop (otherwise you will return to the first iteration of the loop).

  • Change count += [i] to count += i .


Also (not sure if you knew this), range(a, b, 1) will contain all the numbers from a to b - 1 (not b ). In addition, you do not need argument 1 : range(a,b) will have the same effect. Therefore, to contain all numbers from a to b , you must use range(a, b+1) .

Probably the fastest way to add all the even numbers from a to b is

 sum(i for i in xrange(a, b + 1) if not i % 2) 
+5
source

You can make it a lot simpler by correctly using the step argument for the range function.

 def sum_even(a, b): return sum(range(a + a%2, b + 1, 2)) 
+3
source

You do not need a loop; you can use simple algebra:

 def sum_even(a, b): if (a % 2 == 1): a += 1 if (b % 2 == 1): b -= 1 return a * (0.5 - 0.25 * a) + b * (0.25 * b + 0.5) 

Edit:

As NPE pointed out, my original solution above uses floating point math. I was not too worried, since the overhead of floating point math is negligible compared to deleting a loop (e.g. when calling sum_even(10, 10000) ). In addition, the calculations use (negative) degrees equal to two, so they should not be subject to rounding errors.

Anyway, with the simple trick of multiplying all by 4, and then again dividing at the end, we can use integers over everything that is preferable.

 def sum_even(a, b): if (a % 2 == 1): a += 1 if (b % 2 == 1): b -= 1 return (a * (2 - a) + b * (2 + b)) / 4; 
+2
source

Indentation matters in Python. The code you write is returned after processing the first element.

+1
source

I would like you to see how your loops work if b is close to 2 ^ 32 ;-) Since Matthew said there is no need for a loop, but he does not explain why.

The problem is the simple arithmetic sequence of the wiki . The sum of all elements in this sequence:

  (a+b) Sn = ------- * n 2 

where "a" is the first element, the last is "b", and "n" is the number, if elements. If we make even numbers "a" and "b", we can easily solve this problem. Therefore, creating "a" and "b" is equally simple:

 if ((a & 1)==1): a = a + 1 if ((b & 1)==1): b = b - 1 

Now think how many elements we have between two even numbers - this is:

  ba n = --- + 1 2 

Put it in the equation and you get:

  a+b ba Sn = ----- * ( ------ + 1) 2 2 

so that your code looks like this:

 def sum_even(a,b): if ((a & 1)==1): a = a + 1 if ((b & 1)==1): b = b - 1 return ((a+b)/2) * (1+((ba)/2)) 

Of course, you can add code to prevent it from being equal to or greater than b, etc.

+1
source
 def sum_even(a,b): count = 0 for i in range(a, b): if(i % 2 == 0): count += i return count 

Two errors here:

  • add i instead of [i]
  • you return the value directly on the first iteration. Move the countdown from the for loop.
0
source

the sum of all even numbers between the start and end numbers ( inclusive ).

  def addEvenNumbers(start,end): total = 0 if end%2==0: for x in range(start,end): if x%2==0: total+=x return total+end else: for x in range(start,end): if x%2==0: total+=x return total print addEvenNumbers(4,12) 
0
source

This may be an easy way to do this using the range function. the third number in the range is the step number, i.e. 0, 2, 4, 6 ... 100

 sum = 0 for even_number in range(0,102,2): sum += even_number print (sum) 
0
source

a bit more interesting with the advanced python function.

 def sum(a,b): return a + b def evensum(a,b): a = reduce(sum,[x for x in range(a,b) if x %2 ==0]) return a 
0
source

All Articles