isPrime in One Line of Perl

As a follow up to Finding the first 20 terms in the Fibonacci Series, next in the 3Monkey Golf series — Determining if a number is prime. The following is a fairly simple Perl one liner to do the trick. I’m interested in seeing implementations in other languages. I will be posting follow ups with the best submissions from both Golf challenges at the end of the week.

Perl Code

perl -wle'print(1)if(1xtarget_number)!~/^1?$|^(11+?)\1+$/'

Character count = 36

Golf Rules:

  • Only pure source characters are counted, that is, in my example the characters “perl -wle'” and trailing “‘” do not count.
  • For this challenge, the meta variable target_number counts as a single character, butshould be able to be replaced with any reasonable number.
  • Output must be of the form 0, 1, Y, N, true, false, or some other easily recognizable indication of Yes or No.
  • Please note that use og characters such as > and < require escaping as & lt; and & gt;
    If you have difficulty posting your solution please email me the solution at stacy (at) 3monkeyweb (period) com and I will make sure it gets posted.

    Good luck, and until next time.


    Be Sociable, Share!
    1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (4 votes, average: 6.75 out of 10)

    39 Responses to “isPrime in One Line of Perl”

    1. 3Monkeys Says:

      Finally got the ' correct :) It should work now as a copy/paste.

    2. 3Monkeys Says:

      @Bloid, I think that’s where I first found this one. I have several of these tucked away for study.

    3. 3Monkeys Says:

      @Mikhail, that is why I added the “should be able to be replaced with any reasonable number” caveat in the “rules”. You are correct though about perl being able to handle extremely large numbers. I was hoping for a python shot at it as it should be able to handle it.

    4. Daniel Spiewak Says:

    5. payday loan Says:

      You need to take part in a contest for one of the most useful sites on the net. I will recommend this site!

    6. Adrien Montesinos Says:

      One thing I’d prefer to say is that before acquiring more pc memory, consider the machine into which it is installed. In the event the machine is running Windows XP, for instance, the actual memory limit is 3.25GB. Putting in greater than this would just constitute any waste. Make sure one’s motherboard can handle the upgrade volume, as well. Interesting blog post.

    7. Michael Lawerance Says:

      Wow! Outstanding write-up, We cherished it. Do you intend to actually carry out a follow-up blog post? You really ought to come go over my personal internet site as well! you’ll find it’s on the subject of blog.

    8. Doug Vida Says:

      Whoa! Wonderful page, My husband and I enjoyed that. Do you really intend for you to perform a follow-up write-up? You really need to come evaluate my favorite website also! it’s with regards to blogsite.

    9. adult social web sites Says:

      Wow! Superb article, I really enjoyed it all. Do you really plan to actually do a follow-up blog post? You need to come check my rrnternet site as well! you’ll find it’s regarding website.

    10. Reuben Grasso Says:

      This specific artile I just read in forbes with regards to Yahoo! is right on. Proceed Marissa.

    11. Traffik Buster 4 Says:

      traffik buster forum twitter software Generate Thousands of Visitors to your Websites and Offers twitter traffik buster

    12. Mariel Dellenbaugh Says:

      Nice blog right here! Also your web site a lot up fast! What host are you using?

    13. Erma Marchell Says:

      Greetings from California! I’m bored to death at work so I decided to browse your blog on my iphone during lunch break. I really like the information you provide here and can’t wait to take a look when I get home.

    14. obstructive sleep apnea Says:

      Hi to every , because I am really eager of reading this website’s post to be
      updated onn a regular basis. It contains pleasant data.

      Feel free to visit my web page :: obstructive sleep apnea

    15. Leora Says:

      If some one wishes expert view concerning running a blog after that i advise him/her
      to pay a quick visit this weblog, Keep up the fastidious work.

    16. Swapping of Two Numbers Without Using Third Variable Says:

      Youre so cool! I dont suppose Ive read something like this before. So nice to seek out someone with some unique thoughts on this subject. realy thanks for beginning this up. this website is one thing that is wanted on the web, somebody with just a little originality. helpful job for bringing one thing new to the internet!

    17. Posicionamiento web profesional Says:

      Google Adwords: nuevamente como hemos empleado el planificador
      de palabras clave para el proceso del keyword research asimismo nos
      da el número de búsquedas de esa palabra clave, además de la
      competencia de la palabra clave que posee, resulta una herramienta seo imprescindible para sopesar keywords y que deberías de usar sin duda para esta una parte
      del proceso de ponderación de keywords para poder ver cuáles son las mejores que puedo usar.

    18. video marketing Says:

      I’m not positive the place you are getting your info, however great topic.

      I must spend some time studying much more or working out more.
      Thank you for magnificent info I used to be on the
      lookout for this info for my mission.

    19. pounds to euro Says:

      Great post. I waas checking continuously this blolg and I am impressed!

      Extremely useful informatio particularly the lqst part :
      ) I care for such info a lot. I was looking foor this certain info for a
      very long time. Thank you and bst of luck.

    20. bridal makeup artists in chennai|best bridal makeup artists in chennai|bridal makeup artists chennai|top bridal makeup artists chennai|bridal makeup artists in chennai|best bridal makeup artist in chennai|freelance bridal makeup artist in chennai|best bri Says:

      Magnificent items from you, man. I’ve have in mind your stuff previous to and you are just too wonderful. I really like what you have acquired right here, certainly like what you’re stating and the way in which by which you say it. You make it enjoyable and you still care for to stay it smart. I can not wait to learn far more from you. This is really a terrific web site.

    21. Anonymous Says:

      ???????????????????????????????????2001????????????????????????????????nerdery??????????????? ??????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????freaklab????????????????????????????????ulychoc??????????????????

    22. Anonymous Says:


    23. Anonymous Says:

      Gaga Milano???????????? ???????????????????? . 0.267826628 ????Gaga Milano?????: ?? . ????????????????????????????????????????????????????????(^_^)? . ??????????????Blog:

    24. Anonymous Says:

      ??????? ????????? ????????????????????????????????????????+ ?????+???????+?????+?????????????????? ?????????????!????????????????????? ????????????

    25. Anonymous Says:

      ?????Swatch???????????Mr . Kevin Rollenhagen?Swatch??????????????????????????????????????????????Fala??????????????Cilla???????????????????????Linah???Baby????????????Parry????????????????Swatch 2011??????????????????????????New Gent Coloured???????Blooded????????????????????????

    26. Anonymous Says:

      ????????????????????????????????????2??????????????????????????????????????????????????????????????1?2?3?????????????????????????????????????;??????????????????????????????????????????????????????????BOTTEGA VENETA??????????????????????????????????????????????????????????????????????????????????????????????SOS???????????????????????????????????????????????????????????????????????????????????????

    27. Anonymous Says:


    28. Anonymous Says:


    29. Anonymous Says:

      ?????????????????????????????1???????????? 0.266876407 . 0.163217187 ????????????????????????????????????????????????? . 2015??????????????????????????????????????????????????????????????????????????3???????????????????????????????????

    30. Anonymous Says:

      2015?????????????????????????????clé???????????????????????????clé?????????????10???????sihh 2015????1??????????????????????????????????????????????????2015??clé????????18????????????????????????????????????????clé????????????????????????????????????????????????2015???????clé????????????????????????????????????????????????????????

    31. Anonymous Says:

      ???????????? ???2015????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????57??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????18 k??????????????????????????????

    32. Anonymous Says:

      ????????Serpent Calendar???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????;??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Serpent???????????????????????????????Speake-Marin?????????????

    33. Anonymous Says:


    34. Instant Make Money Says:

      Thanks for these tips. One thing I should also believe is credit cards providing a 0% rate of interest often bait consumers along with zero rate, instant acceptance and easy internet balance transfers, however beware of the number one factor that can void your current 0% easy streets annual percentage rate and also throw anybody out into the bad house rapidly.

    35. best seo packages in india Says:

      One more thing. I believe that there are many travel insurance websites of trustworthy companies that let you enter holiday details and get you the prices. You can also purchase an international holiday insurance policy on internet by using your current credit card. Everything you need to do is to enter your current travel details and you can understand the plans side-by-side. Just find the package that suits your finances and needs and after that use your bank credit card to buy it. Travel insurance on the internet is a good way to do investigation for a dependable company for international travel cover. Thanks for sharing your ideas.

    36. Says:

      3monkeys » isPrime in One Line of Perl

    37. Just a moment please... Says:

      Heya i’m for the primary time right here Just a moment please…. I stumbled onto this specific panel and that i still find it truly beneficial & that reduced the problem out a great deal. I really hope to give one thing once more along with help other folks such as you taught me to be.

    38. Jodie Mckell Says:

      I believe this site contains some real great info for everyone :D. “We rarely think people have good sense unless they agree with us.” by Francois de La Rochefoucauld.

    39. Aiko Carreker Says:

      that’s (good/great/wonderful), thanks for your (share/info),.. I think this is (great/amazing) blog

    Leave a Reply