KaliMa
KaliMa

Reputation: 2060

Sum while in Python?

For instance let's say I want to sum up all x^2 (or any other arbitrary function) for x=1 onward until x^2 is greater than n. Can this be done without using a bunch of while loops and if-checks?

Upvotes: 2

Views: 331

Answers (5)

sampson-chen
sampson-chen

Reputation: 47267

Is x incremented by 1 each time?

If so, this reduces to the following formula: x(x+1)(2x+1)/6, since it's sum of the squares of integers.

So you really want to avoid loops, you can solve for x(x+1)(2x+1)/6 < n directly

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54242

sum up all x^2
until x^2 is greater than n

sum = 0
x = 1
while x**2 <= n:
    sum += x**2
    x += 1

One while loop, no ifs.

If you'd rather use a for loop:

import itertools

sum = 0
for x in itertools.count(1): # Generates an infinite series: 1, 2, 3, ...
    square = x**2
    if square > n:
        break
    sum += square

One for loop, one if. Hardly "a bunch".

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208475

The itertools modules has some nice functions for an extensible solution:

from itertools import takewhile, count

def sum_func(func, n):
    return sum(takewhile(lambda x: x < n, (func(i) for i in count(1))))

For example:

>>> sum_func(lambda x: x**2, 20)  # 1^2 + 2^2 + 3^2 + 4^2
30

If you want to make this also work with decreasing functions, you could also just pass in the test function:

def sum_func(func, pred):
    return sum(takewhile(pred, (func(i) for i in count(1))))

Example:

>>> sum_func(lambda x: -x*2, lambda x: x > -10)  # -1*2 + -2*2 + -3*2 + -4*2
-20

Upvotes: 6

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

Absolutely.

>>> sum(x ** 2 for x in itertools.takewhile(lambda x: x ** 2 <= 100, itertools.count(1)))
385

Upvotes: 3

Joran Beasley
Joran Beasley

Reputation: 113988

dont you just need to do ...

max_val = 144   
sum(x**2 for x in range(sqrt(max_val)))

Upvotes: 1

Related Questions