list problem...

Shashwat Anand anand.shashwat at gmail.com
Tue Sep 28 22:09:42 CEST 2010


On Wed, Sep 29, 2010 at 1:15 AM, rog <rog at pynguins.com> wrote:

> Shashwat Anand wrote:
>
>
>>
>> On Wed, Sep 29, 2010 at 12:14 AM, Rog <rog at pynguins.com <mailto:
>> rog at pynguins.com>> wrote:
>>
>>    Hi all,
>>    Have been grappling with a list problem for hours...
>>    a = [2, 3, 4, 5,.....]
>>    b = [4, 8, 2, 6,.....]
>>    Basicly I am trying to place a[0], b[0] in a seperate list
>>    IF a[2] and b[2] is present.
>>
>>
>> You are not exactly clear with your problem statement.
>> Care to elaborate it more.
>>
>>    I have tried sets, zip etc with no success.
>>    I am tackling Euler projects with Python 3.1, with minimal
>>    knowledge, and having to tackle the language as I progress.
>>    Enjoyable frustration :)
>>
>>    --
>>    Rog
>>    http://www.rog.pynguins.com
>>    --
>>    http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>>
>> --
>> ~l0nwlf
>>
>
> "Let d(/n/) be defined as the sum of proper divisors of /n/ (numbers less
> than /n/ which divide evenly into /n/).
> If d(/a/) = /b/ and d(/b/) = /a/, where /a/ ≠ /b/, then /a/ and /b/ are an
> amicable pair and each of /a/ and /b/ are called amicable numbers.
>
> For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44,
> 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4,
> 71 and 142; so d(284) = 220.
>
> Evaluate the sum of all the amicable numbers under 10000."
>
> from Project Euler.
> I have all answers contained in two lists but need to extract the amicable
> pairs.
> eg
> a = [200, 4, 284,.....]
> b = [284, 9, 200, ......]
> Hope that helps.
> Thanks for the link.
>

I am not sure how and what you managed, but what I understand you need to
calculate sum of divisors.
A quick unoptimized solution can be,

>>> def divsum(n):return sum(i for i in range(1, n) if not n % i)... >>> divsum(220)284>>> divsum(284)220


Now all is left is looping and little maths, since upper bound is not
high, brute force is Ok.

<spoiler> http://codepad.org/g6PRyoiV </spoiler>  # For reference

Also I guess you missed 'Reply All', please take care of it next time.


> Rog
>
>


-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100929/5631814f/attachment.html>


More information about the Python-list mailing list