81 chars to recursively construct the 2d array (not including " sub f {}"):
sub f {
#234567890123456789012345678901234567890123456789012345678901234567890
+12345678901
my$x=my$i=pop;$x?([1..$x],map[(map$_+2*$x-1,reverse@$_),++$i],reverse
+f($x-1)):()
}
The first argument is the dimension of the spiral.
## print "@$_\n" for f(5);
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
The recursive pattern is the following: An n x n spiral looks like this:
+---------------+
| 1 2 3 ... n |
| +-------+ n+1 |
| | | . |
| | *** | . |
| | | . |
| +-------+ 2n-1|
+---------------+
where the *** box is the (n-1) x (n-1) spiral, rotated 180 degrees and 2n-1 added to each component.
Update: oops, moved a reverse to the right spot (didn't affect char count).
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, 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, 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.
|
|