Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are some ways to sum the squares of the numbers from 0 to 1000, with timings:

sage: var('x');f = x^2;
sage: timeit('sum(f,x,0,1000)')                 
5 loops, best of 3: 271 ms per loop
sage: timeit('sum([f(x=t) for t in [0..1000]])')
5 loops, best of 3: 39 ms per loop
sage: timeit('sum([x^2 for x in [0..1000]])')   
625 loops, best of 3: 1.21 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,[0..1000],0)')
625 loops, best of 3: 1.13 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,xrange(0,1001),0)')
625 loops, best of 3: 945 µs per loop

So if speed matters to you, you should learn a little python!

For example, the syntax [EXPR for VAR in LIST] is very handy - it evaluates EXPR with VAR ranging over the list (or any iteratable object) LIST. You can even use it with a filtering:

sum([x^2 for x in [0..1000] if is_prime(x)])

will sum the squares of primes in this range.

To understand the last two summation examples above you should read about lambda, reduce, and xrange in a python tutorial. It will pay off!

Here are some ways to sum the squares of the numbers from 0 to 1000, with timings:

sage: var('x');f = x^2;
sage: timeit('sum(f,x,0,1000)')                 
5 loops, best of 3: 271 ms per loop
sage: timeit('sum([f(x=t) for t in [0..1000]])')
5 loops, best of 3: 39 ms per loop
sage: timeit('sum([x^2 for x timeit('sum([t^2 for t in [0..1000]])')   
625 loops, best of 3: 1.21 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,[0..1000],0)')
625 loops, best of 3: 1.13 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,xrange(0,1001),0)')
625 loops, best of 3: 945 µs per loop

So if speed matters to you, you should learn a little python!

For example, the syntax [EXPR for VAR in LIST] is very handy - it evaluates EXPR with VAR ranging over the list (or any iteratable object) LIST. You can even use it with a filtering:

sum([x^2 for x in [0..1000] if is_prime(x)])

will sum the squares of primes in this range.

To understand the last two summation examples above you should read about lambda, reduce, and xrange in a python tutorial. It will pay off!

Here are some ways to sum the squares of the numbers from 0 to 1000, with timings:

sage: var('x');f = x^2;
sage: timeit('sum(f,x,0,1000)')                 
5 loops, best of 3: 271 ms per loop
sage: timeit('sum([f(x=t) for t in [0..1000]])')
5 loops, best of 3: 39 ms per loop
sage: timeit('sum([t^2 for t in [0..1000]])')   
625 loops, best of 3: 1.21 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,[0..1000],0)')
625 loops, best of 3: 1.13 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,xrange(0,1001),0)')
625 loops, best of 3: 945 µs per loop

So if speed matters to you, you should learn a little python!

For example, the syntax [EXPR for VAR in LIST] is very handy - it evaluates EXPR with VAR ranging over the list (or any iteratable iterable object) LIST. You can even use it with a filtering:

sum([x^2 for x in [0..1000] if is_prime(x)])

will sum the squares of primes in this range.

To understand the last two summation examples above you should read about lambda, reduce, and xrange in a python tutorial. It will pay off!