Perl: the Markov chain saw PerlMonks

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

by Arguile (Hermit)
 on May 19, 2001 at 16:14 UTC ( #81706=note: print w/replies, xml ) Need Help??

```sub c{(\$k,@_)=(\$#_)?@_:2..(\$n=@_[0]);@_=map{(\$_%\$k)?\$_:0}@_;push
@_,\$k;\$k<=sqrt(\$n)?c(grep{!/^0/}@_):@_}
```
```[arguile@cobalt ~]\$ wc -L sieve_golf
104 sieve_golf
```
```# display procedure blatantly stolen from Tye

...
100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83
+89 97
169: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83
+89 97 101
103 107 109 113 127 131 137 139 149 151 157 163 167
```
```# non-strict
sub c{\$n=pop;@_=2..\$n;for(\$k=2;\$k<=sqrt(\$n);(\$k)=@_){@_=grep!/^0/,(map
+{(\$_%\$k)?\$_:0}@_),\$k}@_}

# strict w/ less named vars
sub c{pop;@_=2..\$_;for(my\$k=2;\$k<=sqrt;(\$k)=@_){@_=grep!/^0/,(map{\$_%\$
+k?\$_:0}@_),\$k}@_}
```
```sub e{pop;@_=2..\$_;for(my\$k=2;\$k<=sqrt;(\$k)=@_){@_=((grep\$_%\$k,@_),\$k)
+;}@_}
```

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

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

Results (288 votes). Check out past polls.

Notices?