Re: Generators/iterators, Pythonicity, and primes



On 5 Apr., 17:14, John Posner <jjpos...@xxxxxxxx> wrote:
Kay Schluehr said:

> g = (lambda primes = []:
> (n for n in count(2) \
> if
> (lambda n, primes: (n in primes if primes and
n<=primes[-1] \
> else
> (primes.append(n) or True \
> if all(n%p for p in primes if p <= sqrt(n)) \
> else False)))(n, primes)))()

Um ... did you say "easy"? :-) This is prodigious, and it's definitely a
solution to the generator-expression-only challenge I laid down.

That's because it is *one* expression. The avoidance of named
functions makes it look obfuscated or prodigious. Once it is properly
dissected it doesn't look that amazing anymore.

Start with:

(n for n in count(2) if is_prime(n, primes))

The is_prime function has following implementation:

def is_prime(n, primes):
if primes and n<=primes[-1]:
return n in primes
else:
if all(n%p for p in primes if p <= sqrt(n)):
primes.append(n)
return True
else:
return False

But we need a lambda-fied function expression instead of the function
definition. This yields:

is_prime = lambda n, primes: (n in primes \
if primes and n<=primes[-1] \
else (primes.append(n) or True \
if all(n%p for p in primes if p <= sqrt(n)) \
else False))

Finally the trick with primes definition within an expression by means
of an optional argument in the lambda is applied.


But I
think this is a case in which using a generator expression causes a loss
in "Pythonicity", rather than a gain. Many, many thanks for this!

I think so too. The solution with the simple generator expression and
the fully defined is_prime function may be just adequate.
.



Relevant Pages

  • AP-Prime-Generator APPG Re: possible Riemann Hypothesis proof; #137;
    ... rather strange since Euclid's Number in Euclid's Infinitude of Primes ... have devised a Generator of primes, all the primes in that time period. ... So it looks ideal for the Moebius function ... of the Sieve of Eratosthenes. ...
    (sci.math)
  • Re: order of an element
    ... >The question of what order residue 2 has in Z/pZ, p an odd prime, ... >primes, ... >generator of this cyclic group for infinitely many odd primes p. ... >of primes p for which non-square a> 1 is a primitive root). ...
    (sci.math)
  • Re: order of an element
    ... The question of what order residue 2 has in Z/pZ, p an odd prime, ... primes, ... generator of this cyclic group for infinitely many odd primes p. ... of primes p for which non-square a> 1 is a primitive root). ...
    (sci.math)
  • Re: How can I speed up a script that iterates over a large range (600 billion)?
    ... "primes" generator checks to determine whether each number is prime is ... # odd multiple of p; p+p is the increment to get to the next ... # Mark odd nonprimes within each of the requested ranges. ...
    (comp.lang.python)
  • RE: Generators/iterators, Pythonicity, and primes
    ... Subject: Generators/iterators, Pythonicity, and primes ... of an optional argument in the lambda is applied. ... think this is a case in which using a generator expression causes a loss ...
    (comp.lang.python)