Sum a sequence of numbers
Solution 1
A rather quick way to do it would be:
result = 0
for i in range(a,b+1):
if i%2 == 1:
result += i
print result
Solution 2
Sum all the numbers between a
and b
if odd.
sum(i for i in xrange(a, b) if i%2)
Solution 3
There are a bunch of ways to do this. If you think about the math, though, it's a lot like Gauss's old problem. Gauss was asked to add the numbers between 1 and 100, and he realized that each pair of high and low values summed to 101 (100 + 1, 99 + 2, 98 + 3…)
high = b
low = a
So we have to multiply some number of b + a
values. How many are there? For all the integers, that's just
num_pairs = (high-low) // 2
Then we multiply that number by high + low
to get the answer:
result = (high + low) * num_pairs
But you only want every other ones, so we divide by two again:
result //= 2
Totally:
def sumrange(low, high, step):
num_pairs = (high - low) // 2
result = (high + low) * num_pairs
return result // step
or sumrange = lambda low, high, step: (high - low) * (high + low) // (2 * step)
Now this still isn't quite an answer to your question, because it needs to be offset depending on whether your low value is odd, and whether your high value is included or excluded. But I will leave that as an exercise.
Making this a CW answer so someone can edit if my math is messy.
Cloud
Updated on June 04, 2022Comments
-
Cloud almost 2 years
Given: Two positive integers a and b (a
Return: The sum of all odd integers from a through b, inclusively.
#My code: a = 100 b = 200 for i in range(a,b): if i%2 == 1: print i
At the moment it's just showing a drop down list of all the odd integers. I do not know how to affix a "range" to this properly, if need be. How can I add on to my code above to get the sum of all the odd integers?
Thanks
-
glglgl over 10 yearsSummation works with
+
. You sum up values by adding them to a variable, maybesum
. You set this variable to 0 at the beginning and add youri
values to that during the iteration. -
glglgl over 10 yearsBTW, why do you see a drop down list here? I don't see where it would be generated.
-
tobias_k over 10 yearsNote that
range
goes only froma
tob-1
, so you should userange(a, b+1)
. -
Cloud over 10 yearsSorry about the "drop down" list definition. It's rather misleading. I was in a rush. Can anybody suggest a better title perhaps???
-
kojiro over 10 yearsActually this is just a math problem, similar to the one attributed to Gauss. Try something like
sumrange = lambda high, low, step = (high - low) * (high + low) // (2 * step)
-
-
Ashwini Chaudhary over 10 yearsDon't use
sum
as a variable name. -
Cloud over 10 yearsNope. Unable to get the sum. Still getting this initial result:101 103 105 107 109 111 113 115....
-
Cloud over 10 yearsDouble checked and it's still occurring. Any other ways or suggestions?
-
Cloud over 10 yearsAlso: what is the reasoning as to why result=0?
-
LostAvatar over 10 years@Cloud to define a variable to hold the sum?
-
jcfollower over 10 yearsa faster way might be to check if a is odd and if not,
a += 1
and thenfor i in range(a, b+1, 2):
and then don't check for odd numbers anymore. -
jcfollower over 10 yearsdo you need a set of [] or ()?
-
Ohmy over 6 yearsyay for numpy! this solution runs pretty quickly