Sum a sequence of numbers

17,765

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.

Share:
17,765
Cloud
Author by

Cloud

Updated on June 04, 2022

Comments

  • Cloud
    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
      glglgl over 10 years
      Summation works with +. You sum up values by adding them to a variable, maybe sum. You set this variable to 0 at the beginning and add your i values to that during the iteration.
    • glglgl
      glglgl over 10 years
      BTW, why do you see a drop down list here? I don't see where it would be generated.
    • tobias_k
      tobias_k over 10 years
      Note that range goes only from a to b-1, so you should use range(a, b+1).
    • Cloud
      Cloud over 10 years
      Sorry about the "drop down" list definition. It's rather misleading. I was in a rush. Can anybody suggest a better title perhaps???
    • kojiro
      kojiro over 10 years
      Actually 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
    Ashwini Chaudhary over 10 years
    Don't use sum as a variable name.
  • Cloud
    Cloud over 10 years
    Nope. Unable to get the sum. Still getting this initial result:101 103 105 107 109 111 113 115....
  • Cloud
    Cloud over 10 years
    Double checked and it's still occurring. Any other ways or suggestions?
  • Cloud
    Cloud over 10 years
    Also: what is the reasoning as to why result=0?
  • LostAvatar
    LostAvatar over 10 years
    @Cloud to define a variable to hold the sum?
  • jcfollower
    jcfollower over 10 years
    a faster way might be to check if a is odd and if not, a += 1 and then for i in range(a, b+1, 2): and then don't check for odd numbers anymore.
  • jcfollower
    jcfollower over 10 years
    do you need a set of [] or ()?
  • Ohmy
    Ohmy over 6 years
    yay for numpy! this solution runs pretty quickly