On Mon, Nov 8, 2010 at 1:16 AM, Richard D. Moores <rdmoo...@gmail.com> wrote:
> On Sun, Nov 7, 2010 at 15:53, Hugo Arts <hugo.yo...@gmail.com> wrote:
>> n is a proper
>> divisor of x if there is no remainder after division, after all. This
>> also means you won't have to do a cast, which tend to be fairly
>> expensive.
>
> I don't know what a cast is. Would that be the int(n/x)?
>

Yes. A cast or typecast means converting some data to a different
type, like converting floats to integers, strings to integers,
integers to strings, etc. They are fairly expensive because the
representation of the data changes.

> Here are 2 versions, of about equal speed:
>
> def proper_divisors(n):
>    """
>    Return the sum of the proper divisors of positive integer n
>    """
>    return sum([x for x in range(1,n) if n % x == 0])
>
> def proper_divisors(n):
>    sum_ = 0
>    for x in range(1,n):
>        if n % x == 0:
>            sum_ += x
>    return sum_
>
>> On another note, getting rid of the list comprehension and using a
>> generator expression will be even faster, since you won't have to
>> build the list.
>
> Could you spell that out for me?

here's a list comprehension
>>> a = [x*2 for x in range(10)]
>>> a
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

here's the equivalent generator expression:
>>> a = (x*2 for x in range(10))
>>> a
<generator object <genexpr> at 0xb7792fa4>
>>> for x in a: print x
0
2
4
6
8
10
12
14
16
18

As you can see, they are constructed almost the same way, the only
difference being the type of parentheses. The main difference is what
comes out. A list comprehension computes all its elements and builds a
list out of them, very simple.

A generator expression builds a generator. When you create a
generator, nothing is actually done yet. Only when you *iterate* over
a generator (like I did with the for loop) does it start computing
results, one by one, giving each one out as it is needed.

If all you're doing with the results is iterating over them once and
then throwing them away (like you do here with sum()), using a
generator expression saves us some time and space, since we don't have
to build a list object that we won't be needing anyway.

HTH,
Hugo
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to