Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

Re: Calculate prime factors for a given number in a perl one-liner

by moritz (Cardinal)
on Dec 15, 2010 at 18:20 UTC ( #877339=note: print w/ replies, xml ) Need Help??


in reply to Calculate prime factors for a given numer in a perl one-liner

$ perl -E '$_=1x shift;while(/^(11+?)\1+$/){say length$1;$_=1x(length( +)/length$1)}say length' 45 3 3 5

There you go, prime factors in increasing size. I'm sure it can be golfed down even further.


Comment on Re: Calculate prime factors for a given number in a perl one-liner
Download Code
Re^2: Calculate prime factors for a given numer in a perl one-liner
by chrestomanci (Priest) on Dec 15, 2010 at 18:44 UTC

    I don't think you should have provided an answer, especially one without an explanation. Remember this is a homework problem. We are supposed to be helping the supplicant to reach a solution by giving hints, and insightful analysis of the problem.

    For example kennethk pointed out that that there is no need to store prime factors if they will be printed out.

    By giving an answer, you did not help the supplicant, you where just showing off.

      chrestomanci:

      You've written some very good nodes in the brief time (since Nov of this year; 40-some days)... and your view (above) finds support in the Monastery's guidance (so NO - - ), but, in this case

      you're teaching your grandfather how to suck eggs!

      Bishop moritz earned his exalted station, too, and has been here enough longer than you to know the local mores.

      So why have I troubled myself to reply?

      Because moritz' golfed solution, if submitted by a student to satisfy a homework assignment, would reveal itself immediately as work copied from somewhere, not arrived at independently. (In fact, IMO, sometimes in the wake of a particularly lazy question, such a reply is offered in hopes that that lazy one will plagarize and be caught.)

      More often, I suspect, such an answer is offered because it can provide a learning opportunity to the diligent student, even if it does no more than inspire that student to de-obfuscate the expert's code.

      Update: mis-spelling corrected

      I respect your opinion, but I kindly disagree.

      If the OP wanted to learn something from that solution, a quick google search would have supplied insight to actually understand the solution, and to gain something from it.

      I don't see any harm in posting a solution that follows a very different approach to the initial proposal.

      Remember this is a homework problem. We are supposed to be helping the supplicant to reach a solution by giving hints, and insightful analysis of the problem.
      If you want it to be a home-work problem, and to give insightful analysis, you shouldn't ask for a one-liner, and you shouldn't consider Abigail's (infamous) regexp as an answer. It's obscure, inefficient, uses an unreasonable amount of memory, and quite quickly hits limits.
Re^2: Calculate prime factors for a given numer in a perl one-liner
by Utilitarian (Vicar) on Dec 16, 2010 at 09:00 UTC
    I hadn't previously encountered this piece of (lateral|perversity of) thought and so I played about with it a bit. I noticed that once the regex encounters a match greater than 2^16 in length it stops matching. (eg run the command with the argument 131072 (2^17) ).

    Is this a limit of the non-greedy match in regex that I am encountering?

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

      I didn't investigate it thoroughly, but I think it's not the non-greediness. Rather the + in the regex is compiled to {1,32767}, which is a general limit of the 16 bit heritage of the regex engine.

Re^2: Calculate prime factors for a given numer in a perl one-liner
by ambrus (Abbot) on Dec 16, 2010 at 10:14 UTC

      No, that isn't where the technique originated. That node is just me putting it into a nice (and humorous) context. Abigail certainly demonstrated that technique before I posted that. Google says that it "first appeared" on Usenet.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://877339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2014-04-17 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls