Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Prime number generator

by jimt (Chaplain)
on Jul 17, 2006 at 16:14 UTC ( [id://561784]=obfuscated: print w/replies, xml ) Need Help??

My flight out of Baltimore was delayed with mechanical trouble, so I set to work on a one liner to generate prime numbers, in the spirit of One line Fibonacci generator. Unfortunately, I was unable to get it down to a one liner, but it's still pretty damn compact and I am rather proud of it even if it didn't meet my goal of the single statement. 95 characters isn't that bad. And it's pretty fast, too.

++$";while($\=$/and$"++and$;=$"){ do{$;&=$|and last if!($"%$_)}for@_; push@_,$"and print$"if$;}

Replies are listed 'Best First'.
Re: Prime number generator
by liverpole (Monsignor) on Jul 17, 2006 at 19:06 UTC
    Hi jimt,

    Here's a 23-character script that, for any integer input, will output a prime number:

    $_=$=-(shift)**$[;print

    Okay, maybe that's cheating ... :-)

    Here's a prime-number generator in 65 characters.  I wouldn't be surprised if it can be golfed further...

    $o=1;{++$o&&grep{$o/$_==int$o/$_}keys%_ or$_{$o}=print$o.$/;redo}

    Update:  Make that 64 characters...

    $o=1;{++$o&&grep$o/$_==int$o/$_,keys%_ or$_{$o}=print$o.$/;redo}

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      ..and even 57:
      $o=1;{++$o&&grep!($o%$_),keys%_ or$_{$o}=print$o.$/;redo}

           s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
        Very slick! ++

        It looks even nicer, I think, without whitespace, though I can't find a way to improve on your 57 ... yet! ...

        $o=1;{$_{$o}=print$o.$/if++$o&&!grep!($o%$_),keys%_;redo}

        Update:  Aha!  It took me some thinking, but here's 56 ...

        $o=1;{$_{$o}=print$o.$/if++$o,!grep!($o%$_),keys%_;redo}

        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Prime number generator
by rhesa (Vicar) on Jul 17, 2006 at 20:33 UTC
    $o=1;{(grep!($o%$_),2..++$o/2)||print$o.$/;redo}
    That's 48.

    if we allow a shebang line:

    #!/usr/bin/perl -l $o=1;{(grep!($o%$_),2..++$o/2)||print$o;redo}
    45 :)
      Stunning! ++

      But you realize, of course ...

      #!/usr/bin/perl -l $o=1;{(grep!($o%$_),2..$o++)||print$o;redo}

      That's 43.

      It's a little slower (but then we sacrificed speed a while back ;-))

      Update:  Make that 42 ...

      #!/usr/bin/perl -l $o=1;{grep!($o%$_),2..$o++or print$o;redo}

      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Hey, making it post-increment is clever! Well spotted :)

        I had a hard time getting those stupid brackets out of there. At some point, I realised that I was being bitten by buffered output, and didn't think about them anymore.

        I think 42 is perfect!

        Cool :) ++

             s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
Re: Prime number generator
by Ieronim (Friar) on Jul 17, 2006 at 21:47 UTC
    Just for the sake of completeness - my 58-char sieve of Eratosthenes. The one-liner prints all integers less than given argument.
    @_=(2..shift);while(@_){print$_[0].$/;@_=grep{$_%$_[0]}@_}

         s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
Re: Prime number generator
by truedfx (Monk) on Jul 22, 2006 at 11:42 UTC

    I have no idea who it was that originally came up with the idea of regexes, but I liked it and it seems appropriate here, so:

    #!perl -p $_ x=(1x$_)!~/^(|.|(..+)\2+)$/
    $ seq -50 50 | perl prime.pl 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Re: Prime number generator
by mr_mischief (Monsignor) on Jul 22, 2006 at 00:55 UTC
    I'm getting behavior our of perl I can't explain. Is this acting strangely for anyone else? The following one doesn't print a thing:

    {for(2..$p){$i%$_ or$p=0}print$i if$p;$p=$i++,redo}
    and neither does:
    {for(2..$p){$i%$_ or$p=0}print"$i" if$p;$p=$i++,redo}
    but when I try this:
    {for(2..$p){$i%$_ or$p=0}print"$i$/" if$p;$p=$i++,redo}
    it works just fine. I'm on perl 5.8.7 on both Mandriva Linux and ActiveState.


    Christopher E. Stith
      Are you using the -l command line switch? Are you patient enough? Buffering might prevent the output from appearing straight-ahead. Try adding a $|++; to the start of first two alternatives to make it autoflush.
        It was the buffering. Autoflush made it work like a champ. I simply hadn't considered that. Thanks.


        Christopher E. Stith

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://561784]
Approved by VSarkiss
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found