CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
one liner script to find prime numbers between 1 to 100

 



arunendra
New User

Mar 11, 2013, 10:11 PM

Post #1 of 6 (958 views)
one liner script to find prime numbers between 1 to 100 Can't Post

I am trying to write a script to find prime numbers between 1 to 100. And the concern is , I want to write in very small like as below one liner shell script:-

$ echo (1..100)| factor | y= 2 | echo $y


Rahul6990
Novice

Mar 12, 2013, 2:55 AM

Post #2 of 6 (953 views)
Re: [arunendra] one liner script to find prime numbers between 1 to 100 [In reply to] Can't Post

Use Below code:


Code
perl -lne '(1x$_) =~ /^1?$|^(11+?)\1+$/ || print "$_ is prime"'

Source:
http://www.catonmat.net/blog/perl-regex-that-matches-prime-numbers/


Kenosis
User

Mar 12, 2013, 9:45 AM

Post #3 of 6 (946 views)
Re: [arunendra] one liner script to find prime numbers between 1 to 100 [In reply to] Can't Post

The following's adapted from perlmonks:

Code
perl -e '$o=1;{grep!($o%$_),2..$o++or print"$o\n";redo if$o<100}'



Kenosis
User

Mar 12, 2013, 9:54 AM

Post #4 of 6 (945 views)
Re: [Rahul6990] one liner script to find prime numbers between 1 to 100 [In reply to] Can't Post

Modify if just a little for it to generate the range of primes:

Code
perl -e '$_=1;(1x$_) =~ /^1?$|^(11+?)\1+$/ || print "$_ is prime\n" while $_++<100'



Laurent_R
Veteran / Moderator

Mar 12, 2013, 10:59 AM

Post #5 of 6 (940 views)
Re: [Kenosis] one liner script to find prime numbers between 1 to 100 [In reply to] Can't Post

This style would be more Perlish:


Code
perl -e '$_=1;(1x$_) =~ /^1?$|^(11+?)\1+$/ || print "$_ is prime\n" for (1..100)'


Wink


Kenosis
User

Mar 12, 2013, 11:23 AM

Post #6 of 6 (937 views)
Re: [Laurent_R] one liner script to find prime numbers between 1 to 100 [In reply to] Can't Post

Then you can omit the initial:

Code
$_=1;

And use:

Code
for 1..100

Smile


(This post was edited by Kenosis on Mar 12, 2013, 11:25 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives