xorcist
xorcist

Reputation: 43

Find total sum and sum of numbers in range based on condition

I want to create a function my_sum_div(a, b, div=10) which will compute the sum of numbers from a to b, prints out this sum, but also all intermediate sums of type a+(a+1)+(a+2)+(a+3) that are divisible by div. I want the program to work as follows:

    my_sum_div(1, 10, 2)

The output should look like:

sums divisible by 2:
6
10
28
36
sum of numbers between 1 and 10 is: 55

Now I have already done this:

    def my_sum(a, b):
    print("sum of numbers between", a," and", b," is", sum(i for i in 
    range(a, b+1)))

    def my_sum_div(a, b, div = 3):
    print("sums divisible by", div,":")

    l = []
    s = ","
    x = sum(i for i in range(a, b+1)
    for i in range(a, b+1):
        if (i%div==0):
            l.append(str(i))

    print(s.join(l))

    print("sum of numbers between", a, " and", b, " is", sum(i for i in 
    range(a, b + 1))

Ive been trying for hours. I know my code only checks what numbers between (a, b+1) are divisible by div, but how do I make it so that the function computes and prints all the intermediate sums that are divisible by div? My first function only computes the sum of all integers in the given interval. I now want the subsequent function to compute all intermediate sums that are divisible by div in a given interval and print all of these.

Thank you in advance.

Upvotes: 1

Views: 1164

Answers (3)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48057

You may create your custom generator to yield the results as:

def my_sum_div(start, end, d):
    my_sum = 0
    for x in range(start, end+1):
        my_sum += x 
        if my_sum % d == 0:
            yield my_sum
    # yield my_sum     # <-- uncomment this if you want the total 
                       #     sum to be returned at the end

Using above generator, you may yield one result at a time with next() as:

>>> my_generator = my_sum_div(1, 10, 2)
>>> next(my_generator)
6
>>> next(my_generator)
10
>>> next(my_generator)
28

OR you may get the list of all the results in one go as:

>>> list(my_sum_div(1, 10, 2))
[6, 10, 28, 36]

Result with commented version of code to yield total my_sum at last:

>>> list(my_sum_div(1, 10, 2))
[6, 10, 28, 36, 55]

In Python 3, you can simply store the sum of intermediate values and the total sum at the end in two variables as:

>>> *intermediate_sum, total_sum = list(my_sum_div(1, 10, 2))
# where: 
#  - `intermediate_sum` will hold the value `[6, 10, 28, 36]`
#  - `total_sum` will hold the value `55`

For older versions of Python, you need to do:

>>> sum_list = list(my_sum_div(1, 10, 2))
>>> intermediate_sum, total_sum = sum_list[:-1], sum_list[-1]

Upvotes: 1

Tien Dinh
Tien Dinh

Reputation: 361

Here’s my implementation

def my_sum(a, b):
    return sum(i for i in range(a, b+1)))

def my_sum_div(a, b, div = 3):
    print("sums divisible by", div,":")

    l = []
    s = ","
    test = 0
    for i in range(a, b+1):
        test += i
        if (test%div==0):
            l.append(str(test))

    print(s.join(l))

    print("sum of numbers between", a, " and", b, " is", my_sum(a, b))

Upvotes: 0

CrizR
CrizR

Reputation: 688

def my_sum_div(a,b,div):
        sum = 0
        for i in range(a, b + 1):
            sum += i
            if sum % div == 0:
                print(sum)
        return sum

print(my_sum_div(1,10,2))

Output:

6
10
28
36
55

Upvotes: 1

Related Questions