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

Re: Spiraling integers

by hdb (Monsignor)
on Jan 22, 2014 at 09:02 UTC ( #1071576=note: print w/replies, xml ) Need Help??


in reply to Spiraling integers

Here is my ASCII version of Ulam's spiral, ie. spiralling outwards not inwards.

use strict; use warnings; use Math::Prime::XS 'sieve_primes'; my $n = shift; my %primes = map { $_ => 'O' } sieve_primes $n; my @spiral; my $o = 1 + int( sqrt( $n )/2 ); my ( $x, $y ) = ( $o, $o ); # starting point my ( $xs, $ys ) = ( 1, 0 ); # initial direction my $flip = 1; # change of direction indicator my $m = 1; # length of current lag for my $i ( 1..$n ) { $spiral[$y][$x] = $primes{ $i }//' '; $x += $xs; $y += $ys; ($xs, $ys) = ( ( $flip=1-$flip ) and $m++ ) ? ($ys, 0) : (0, -$xs) + unless $i%$m; } $spiral[$o][$o] = 'X'; print join '', map { $_//' '} @$_, "\n" for @spiral;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2019-02-21 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (108 votes). Check out past polls.

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