Reputation: 131
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
An example I put was print(sum_even(3,7)) and the output is 0. I cannot figure out what is wrong.
Upvotes: 4
Views: 40202
Reputation: 1524
def sum_even_numbers(n):
k = n // 2
return k * (k + 1)
To sum even numbers from 1 to a specific number 𝑛 in O(1) you can use above code.
since the numbers till a certain point half even and odd we take half which is even and perform the law that give us the even numbers till a point which is n*(n+1)
Upvotes: 0
Reputation: 411
List based approach,
Use b+1
if you want to include last value.
def sum_even(a, b):
even = [x for x in range (a, b) if x%2 ==0 ]
return sum(even)
print(sum_even(3,6))
4
[Program finished]
Upvotes: 0
Reputation: 3
SUM of even numbers including min and max numbers:
def sum_evens(minimum, maximum):
sum=0
for i in range(minimum, maximum+1):
if i%2==0:
sum = sum +i
i= i+1
return sum
print(sum_evens(2, 6))
OUTPUT is : 12
sum_evens(2, 6) -> 12 (2 + 4 + 6 = 12)
Upvotes: 0
Reputation: 1
This will add up all your even values between 1 and 10 and output the answer which is stored in the variable x
x = 0
for i in range (1,10):
if i %2 == 0:
x = x+1
print(x)
Upvotes: -1
Reputation: 20640
You don't need the 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 maths. I wasn't too concerned, since the overhead of floating-point maths is negligible compared with the removal of the looping (e.g. if calling sum_even(10, 10000)
). Furthermore, the calculations use (negative) powers of two, so shouldn't be subject by rounding errors.
Anyhow, with the simple trick of multiplying everything by 4 and then dividing again at the end we can use integers throughout, which 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
Upvotes: 3
Reputation: 96
little bit more fancy with advanced python feature.
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
Upvotes: 0
Reputation: 11
This might be a simple way of doing it using the range function. the third number in range is a step number, i.e, 0, 2, 4, 6...100
sum = 0
for even_number in range(0,102,2):
sum += even_number
print (sum)
Upvotes: 1
Reputation: 1
The sum of all the even numbers between the start and end number (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)
Upvotes: 0
Reputation: 7267
I'd like you see how your loops work if b is close to 2^32 ;-) As Matthew said there is no loop needed but he does not explain why.
The problem is just simple arithmetic sequence wiki. Sum of all items in such sequence is:
(a+b)
Sn = ------- * n
2
where 'a' is a first item, 'b' is last and 'n' is number if items. If we make 'a' and b' even numbers we can easily solve given problem. So making 'a' and 'b' even is just:
if ((a & 1)==1):
a = a + 1
if ((b & 1)==1):
b = b - 1
Now think how many items do we have between two even numbers - it is:
b-a
n = --- + 1
2
Put it into equation and you get:
a+b b-a
Sn = ----- * ( ------ + 1)
2 2
so your code looks like:
def sum_even(a,b):
if ((a & 1)==1):
a = a + 1
if ((b & 1)==1):
b = b - 1
return ((a+b)/2) * (1+((b-a)/2))
Of course you may add some code to prevent a be equal or bigger than b etc.
Upvotes: 2
Reputation: 129587
Move the return
statement out of the scope of the for
loop (otherwise you will return on the first loop iteration).
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
). Moreover, you don't need the 1
argument: range(a,b)
will have the same effect. So to contain all the numbers from a
to b
you should use range(a, b+1)
.
Probably the quickest 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)
Upvotes: 5
Reputation: 870
You can make it far simpler than that, by properly using the step argument to the range function.
def sum_even(a, b):
return sum(range(a + a%2, b + 1, 2))
Upvotes: 4
Reputation: 47367
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
doesn't get scoped to your for loop (in which case it would return on the 1st iteration, causing it to return 0)
(And change [i]
to i
)
NOTE: another problem - you should be careful about using range
:
>>> range(3,7)
[3, 4, 5, 6]
so if you were to do calls to:
sum_even(3,7)
sum_even(3,8)
right now, they would both output 10
, which is incorrect for sum of even integers between 3 and 8, inclusive.
What you really want is probably this instead:
def sum_even(a, b):
return sum(i for i in range(a, b + 1) if i % 2 == 0)
Upvotes: 10
Reputation: 4855
def sum_even(a,b):
count = 0
for i in range(a, b):
if(i % 2 == 0):
count += i
return count
Two mistakes here :
Upvotes: 0
Reputation: 25484
Indentation matters in Python. The code you write returns after the first item processed.
Upvotes: 1