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

by dhaval (Initiate)
 on Dec 20, 2010 at 13:37 UTC ( #878010=note: 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?
```  ^                # 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.
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};

Replies are listed 'Best First'.
Re^2: Calculate prime factors for a given numer in a perl one-liner
by bhumip (Initiate) on Dec 20, 2010 at 13:54 UTC
Thanks for sharing your knowledge. Kudos for performance based solution
Just for info, I did the same course. Didn't googled for a solution. I've come with that solution. Now I'm googling it just to see how other perl user are solving that easy problem :-) and what I see is quite interesting (especially this things with regex!!) It might not be the speediest, but still it might be of some interest to some people!
```perl -le '\$_=2;while(\$ARGV[0]-1){if(!(\$ARGV[0]%\$_)){print\$_ ;\$ARGV[0]/
+=\$_;}else{\$_++}}' \$1
or in a non-one-liner form:
```
\$_=2;
while (\$ARGV[0]-1) {
if (!(\$ARGV[0]%\$_)) {
print \$_ ;
\$ARGV[0]/=\$_;
} else {
\$_++
}
}
Sincerely yours, Alessandro

Create A New User
Node Status?
node history
Node Type: note [id://878010]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-08-20 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (316 votes). Check out past polls.

Notices?