1
\$\begingroup\$

This is a function which produces the sum of primes beneath (not including) a certain number (using a variant on the sieve of Eratosthenes).

erastosum = lambda x: sum([i for i in xrange(2, x) if i == 2 or i == 3 or reduce(lambda y,z: y*z, [i%j for j in xrange(2,int(i ** 0.5) + 1)])])

Excessive use of lambda? Perhaps. Beautification would be nice, but I'm looking for performance optimizations. Sadly, I'm not sure if there is any further way to optimize the setup I've got right now, so any suggestions (on how to, or what else to do) would be nice.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 12, 2011 at 0:09
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

As much as I love anonymous functions, they can be a nightmare to debug. Splitting this code up piece wise (into an actual function or otherwise) shouldn't and wouldn't decrease it's performance while improving maintenance and portability for you later on.

This class is quite efficient for determining primes. Despite being quite lengthy is more efficient than the more usual approach.

answered Nov 12, 2011 at 5:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.