Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Spiraling integers

by lidden (Curate)
on Aug 28, 2005 at 01:30 UTC ( #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
Node Status?
node history
Node Type: note [id://487210]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2019-10-21 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?