Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Elijah,
I am sure there is a mathematical equation that would simplify this but I was spending too much time with my TI-89 Titanium trying to figure it out so...

There is an underlying pattern that can be reduced to simple enough instructions to end up with a single array of N2 elements populated iteratively. It didn't take me too long to figure it out once I flattened a couple of starting lists with their corresponding solutions

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 01 02 03 04 12 13 15 05 11 16 15 06 10 09 08 07 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 +4 25 01 02 03 04 05 16 17 18 19 06 15 24 25 20 07 14 23 22 21 08 13 12 11 1 +0 09
You can see the solution below:
#!/usr/bin/perl use strict; use warnings; my $n = $ARGV[0] || 5; my @spiral = gen_spiral($n); output(@spiral); sub output { my @spiral = @_; my $n = sqrt @spiral; my $cnt; for ( @spiral ) { print "\n" if $cnt++ % $n == 0; printf("%-3d ", $_); } } sub gen_spiral { my $n = shift; my ($tot, @spiral) = ($n * $n, ()); my ($index, $cnt, $l_border, $r_border, $fill_size) = (1, 1, 1, $t +ot, $n + 1); while ( $cnt <= $tot ) { ##### Moving right ##### # Step 1, decrease fill size --$fill_size; # Step 2, fill in for ( 1 .. $fill_size ) { $spiral[ $index++ ] = $cnt++; } --$index; # Step 3, skip by n's for ( 1 .. $fill_size - 1 ) { $index += $n; $spiral[$index] = $cnt++; } ##### Moving left ##### # Step 1, decrease fill size --$fill_size; # Step 2, fill in for ( 1 .. $fill_size ) { $spiral[ --$index ] = $cnt++; } # Step 4, skip by n's for ( 1 .. $fill_size - 1 ) { $index -= $n; $spiral[$index] = $cnt++; } ++$index; } shift @spiral; # Arrays are 0 based return @spiral; }

Cheers - L~R


In reply to Re: Spiraling integers by Limbic~Region
in thread Spiraling integers by Elijah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2019-02-20 13:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I use postfix dereferencing ...









      Results (106 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!