Just another Perl shrine PerlMonks

### Re: Re: (Golf): Sieve of Eratosthenes

by chipmunk (Parson)
 on May 19, 2001 at 21:54 UTC ( #81740=note: print w/replies, xml ) Need Help??

in reply to Re: (Golf): Sieve of Eratosthenes
in thread (Golf): Sieve of Eratosthenes

I think that undef is not prime. :)

But at 47 characters I think MeowChow still won't be beat:

```sub sieve {
sub p{\$_[0],@_>1?p(grep\$_%\$_[0],@_):()}p 2..pop
}

Replies are listed 'Best First'.
Re: Re: Re: (Golf): Sieve of Eratosthenes
by MeowChow (Vicar) on May 19, 2001 at 21:56 UTC
I had thought of that, but assumed it would slide :)

Make that 46 by the way...

```sub sieve {
sub p{\$_[0],\$#_?p(grep\$_%\$_[0],@_):()}p 2..pop
}
```   MeowChow
s aamecha.s a..a\u\$&owag.print```
I had thought of that, but that goes into deep recursion if you call sieve(1). :)

Oh, but tilly says that you may assume \$n is above a fixed number. Pooh!

No. I only say that for the bonus problem.

The first problem is correct first, efficient afterwards. The second should express the efficient algorithm accurately, but I allow some boundary sloppiness.

Re: Re: Re: (Golf): Sieve of Eratosthenes
by MeowChow (Vicar) on May 20, 2001 at 02:21 UTC
Abusing the rules for fun and profit (46 chars):
```sub sieve {
sub p{\$_[0],@_>1?p(grep\$_%\$_[0],@_):1}p 2..pop
}
```   MeowChow
s aamecha.s a..a\u\$&owag.print```

Create A New User
Node Status?
node history
Node Type: note [id://81740]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2017-08-23 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (345 votes). Check out past polls.

Notices?