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$..$/
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
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$..$/
| [reply] [d/l] [select] |
|
|
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 :) | [reply] [d/l] [select] |
|
#!/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$..$/
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
It was the buffering. Autoflush made it work like a champ. I simply hadn't considered that. Thanks.
| [reply] |