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

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 (2054 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 (2049 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 (2042 views)
 Re: [arunendra] one liner script to find prime numbers between 1 to 100 [In reply to] Can't Post
 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 (2041 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 (2036 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)'`

Kenosis
User

Mar 12, 2013, 11:23 AM

Post #6 of 6 (2033 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`

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

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0