Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Spiraling integers

by lidden (Curate)
on Aug 28, 2005 at 01:30 UTC ( [id://487210]=note: print w/replies, xml ) Need Help??


in reply to Spiraling integers

I had to spiral too.
my $n = shift; my @AoA; my $flip = 0; my @directions = ( 'right', 'down', 'left', 'up'); my $direction = $directions[ $flip % 4 ]; my ($x, $y) = (0, 0); for ( 1 .. $n * $n ){ $AoA[ $x ][ $y ] = $_; $flip++ if 'right' eq $direction and ( $x == $n - 1 or $AoA[ $x + +1 ][ $y ]); $flip++ if 'down' eq $direction and ( $y == $n - 1 or $AoA[ $x ][ + $y + 1 ]); $flip++ if 'left' eq $direction and ( $x == 0 or $AoA[ $x - +1 ][ $y ]); $flip++ if 'up' eq $direction and ( $AoA[ $x ][ + $y - 1 ]); $direction = $directions[ $flip % 4 ]; $x++ if 'right' eq $direction; $y++ if 'down' eq $direction; $x-- if 'left' eq $direction; $y-- if 'up' eq $direction; } for my $y (0..$n-1){ for my $x (0..$n-1){ printf "%5d ", $AoA[$x][$y]; # for some value of 5 } print "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://487210]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-28 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found