Keep It Simple, Stupid PerlMonks

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

by kudra (Vicar)
 on May 19, 2001 at 15:44 UTC ( #81705=note: print w/replies, xml ) Need Help??

in reply to (Golf): Sieve of Eratosthenes

Much worse than the previous offering and just plain bad approaches (what can I say--I'm bad at golf and I wanted to play with using an array. look here if you want to see some shorter solutions):

```# Example use: @result = sieve(60);
sub sieve {
my\$m=pop;my@l=(0..\$m);for(my\$x=0;\$x<\$m;\$x++){next if(\$l[\$x]<2);my \$i=2
+;my\$n=0;for(;\$n<\$m;\$i++){\$n=\$x*\$i;next if(\$n>\$m);\$l[\$
n]=0}}@l=grep{\$l[\$_]}(0..\$m)
}
152.

```# Example of use: sieve(60);

sub sieve {
my\$m=pop;@_=(2..\$m);for my \$c(@_){my%b=map{\$_,1}grep{(\$_%\$c)==0}(\$c+1.
+.\$m);@_=grep{!\$b{\$_}}@_}die"@_\n"
}
104.

Update

```# Example of use: @result = sieve(60);

sub sieve{
L:for(2..pop){for my \$b(2..\$_-1){next L if(\$_%\$b<1)}push@_,\$_}@_
}

65. Does not mark of multiples but tests each number (only as far as needed, at least), so not really allowed.

Create A New User
Node Status?
node history
Node Type: note [id://81705]
help
Chatterbox?
 [GotToBTru]: late to the conversation .. but thanks, Corion, for the reminder about Perl::Tidy [Corion]: GotToBTru: Heh ;) I'm not really endorsing it, as I found that any gains made through it are easily squandered by the hours spent on configuring it. At least in my case :) [GotToBTru]: Like most Swiss Army Knives, you can cut yourself trying to find the blade you actually want [GotToBTru]: i am satisfied with the default settings turning the seemingly random formatting into something reliable [Corion]: GotToBTru: Yeah, I started a quick(ly implemented but slowly running) implementation of a program that would guess the "best" configuration (that is, least amount of changes) from my code, but then found that it stops parsing the source as soon as it .. [GotToBTru]: sadly, it can't do anything about this [Corion]: ... encounters sub foo(\$bar,\$baz) {, which is something I like nowadays

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-02-27 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Before electricity was invented, what was the Electric Eel called?

Results (387 votes). Check out past polls.