Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: (Golf): Sieve of Eratosthenes

by srawls (Friar)
on May 19, 2001 at 22:17 UTC ( #81746=note: print w/ replies, xml ) Need Help??


in reply to (Golf): Sieve of Eratosthenes

This is my very first golf attempt, and I know it's pretty bad, but here it goes:

Well, it does return duplicates (2 2 5 5 7 7) since I use a hash, but I didn't see anything in the problem that said you couldn't. I count 77 chars (see update). (Note: that is excluding the new-lines, I ran it with out them, but I posted new lines here to be more pleasing to the eye, also, it is excluding the sub a { and the end } parts).

sub a { @a{2..$_[0]}=2..$_[0]; for$b(%a){!($b%$_)&&$b!=$_&&delete@a{$b}for(%a)} %a }
One last note, since this is my first golf, I would very much appreciate any suggestions or improvements to my code. I realize I took a very straight forward approach to this solution, but that is how I think.

Update:Took it down to 73 chars. I took out one set of un-needed parenentheses and two un-needed semicolons.

Note:If you put keys before the %a it takes away the problem of repeats, but it brings me back to where I started with 77 chars.

The 15 year old, freshman programmer,
Stephen Rawls


Comment on Re: (Golf): Sieve of Eratosthenes
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-29 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls