Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It seems a little bit much to ask in my opinion of a one-liner, but what do you think? Part of my question is whether this is a good quiz or not.

As per my opinion, quiz is used to measure growth in knowledge and/or skills. A positive attempt to solve the quiz will definitely improve the knowledge even is not solved. So in case of one-liner task, It might improve the in-depth knowledge of language.

I think I understand the brilliance behind the regular expression, and turning the number into a string, I just have one question about the math. The reg-ex will match the smallest possible factor, right? Each iteration of the loop it effectively divides the length of the string by the match's length. If it does match the smallest possible factor (or exits the loop and prints the length), then how does it ensure that it only picks prime numbers each time?

Yes, Its a brilliant use of regular expression, a string operation to fulfill mathematic task.

then how does it ensure that it only picks prime numbers each time?
Please read comment which might help you to understand it
^ # match beginning of string ( # begin first stored group 1 # match a one 1+? # then match one or more ones, minimally. ) # end storing first group \1+ # match the first group, repeated one or more times +. $ # match end of string.
Please read this link http://montreal.pm.org/tech/neil_kandalgaonkar.shtml It will explain it how it works with example.
But as I believe in solution, so performance comes into my mind and I prefer to do it in mathematical way instead of using a regular expression. Please check following code which takes less time then others.
$y=shift; for($i=2;$i<=$y;){ next if$y%$i++; $y/=--$i; push@x,$i } print@{$,=x};
One liner format look like :
$y=shift;for($i=2;$i<=$y;){next if$y%$i++;$y/=--$i;push@x,$i}print@{$, +=x};


In reply to Re: Calculate prime factors for a given numer in a perl one-liner by dhaval
in thread Calculate prime factors for a given numer in a perl one-liner by andmott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found