Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

(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) }

# 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" }

Too bad we're not playing basketball.


# 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.

Comment on (kudra: basketball) Re: (Golf): Sieve of Eratosthenes
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://81705]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2015-11-26 15:32 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (701 votes), past polls