5.2.4 A simple loop example: Sum of squares

The following Sage worksheet illustrates the above sums of squares examples in the Sage Notebook.

Note, we use the notation:

{{{
  INPUT TEXT
///
  OUTPUT TEXT
}}}
to denote cells in the notebook.

{{{
two = int(2)
def sumsquarespy(n):
    return sum(i**two for i in xrange(1,n+1)) 
}}}
{{{
time v=[sumsquarespy(100) for _ in xrange(10000)]
///
#> CPU time: 0.49 s,  Wall time: 0.49 s
}}}
{{{
%cython
# Next in cython.
def sumsquares(int n):
    cdef int i, j
    j = 0
    for i from 1 <= i <= n:
        j = j + i*i
    return j
}}}
{{{
time v=[sumsquares(100) for _ in xrange(10000)]
///
CPU time: 0.06 s,  Wall time: 0.06 s
}}}

See About this document... for information on suggesting changes.