[Tutor] Need help with script for finding pairs of amicable numbers
Richard D. Moores
rdmoores at gmail.com
Wed Nov 17 17:34:17 CET 2010
I got no response to my post that began this thread 8 days ago. I
followed that with a new thread, "List comprehension question" that
continued for 60+ posts, and from which I learned a lot -- about
optimizing a function (and the importance of timing the various
candidates for improvement). The function has been radically changed
and speeded up more than 10x. I didn't mention what it was for, but
now here it is in my script to find Amicable Pairs (ta daa!). See
<http://tutoree7.pastebin.com/wKvMAWpT>. I've used it to find the
first 195 pairs, or all pairs (n, m), n < m where 1 <= n <=
55,000,000. The list is at <http://tutoree7.pastebin.com/gyMar6H2>.
Now, I have no reason at all to care about Amicable Pairs, but I'm
interested in learning how to make the search for them more efficient.
When I had found all pairs up through n = 34,000,000, I thought I'd
found one way that would find APs faster (by one-third) by eliminating
all odd n's that didn't end in '5' (40% of all n's). In fact, up to n
= 34 million, there are no APs whose n is both odd and whose last
digit is not '5'. So I asked on <http://math.stackexchange.com/> if it
is true for all APs: <http://tinyurl.com/37ug3x2>. As you can see, I
should have gone another million out, because that's where the
counterexample (34765731, 36939357) appears. And it remains the only
counterexample up through n = 55 million.
So, Tutors, what can I do to make the search for APs more efficient? C
Smith has pointed out that if I installed sympy, I could do
>>> from sympy import divisors
>>> list(divisors(256))
[1, 2, 4, 8, 16, 32, 64, 128, 256]
and the sum of the proper divisors of 256 would be just
sum(divisors(n)) - n. However, when I did install sympy, and tried
Smith's idea, the result was a major slowdown on my system, for some
unknown reason.
But that's fine, because I wouldn't learn much from sympy anyway,
other than that it performs magic.
I think what I want are ways to eliminate (filter out?) some of the
n's (as in my idea of the odd n's that don't end in '5'). Anyone?
Dick Moores
More information about the Tutor
mailing list