We don't bite newbies here... much 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??

```\$ 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

Replies are listed 'Best First'.
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

Create A New User
Node Status?
node history
Node Type: note [id://877339]
help
Chatterbox?
 [Corion]: 1nickt: Finding autobox in production would give me pause, yes [MidLifeXis]: And under MINGW64_NT-6.1 MYHOST 2.6.0(0.304/5/3) 2016-09-09 09:46 x86_64 Msys there seem to be issues with escapes in external build tool calls. [Corion]: I mean, it's a technical feat it achieves, but... why? ;) [MidLifeXis]: And it also has the 0.14 version of the tarball in its manifest. [LanX]: avoiding unreadable brackets [MidLifeXis]: Although the previous one could be a b0rken PATH, I would need to dig for that. [thezip]: I've got to go to meetings now. If anyone has further comments regarding Spreadsheet::XLSX deployment to Strawberry Perl 5.24.1, please /msg me -- thanks! LanX has to go ... plans to crash with a car into a group of pythonistas while screaming "LARRY IS THE GREATEST" LanX ... darn ... where is my car?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-03-23 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (291 votes). Check out past polls.