Here's mine just to prove I can, but it's much sloppier than most of the examples given here. I'll have to analyze your code and see if I can make mine more efficient.
sub permute {
return if $_[0] && $_[0] / length($_[0]) != int ($_[0] / length($_
+[0]));
if (!length($_[1])) { print $_[0]." "; return; }
for (0..length($_[1])-1) {
permute($_[0].substr($_[1],$_,1), substr($_[1],0,$_).substr($
+_[1],$_+1));
}
}
Incidently, I tried some larger numbers and found that the following are all the matches up through 19 digits:
1 : 1
12 : 12
123 : 123 321
123456 : 123654 321654
12345678 : 38165472
123456789 : 381654729
1234567890 : 3816547290
Then you hit 20 and start getting piles of matches. My algorithm isn't efficient enough yet to go beyond 21 or so.
EDIT:
si_lence gets ~27 iterations per second
Ted_Pride gets ~100 iterations per second
tilly gets ~154 iterations per second
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.