spiral

Build a matrix of numbers, which will be presented in the form of a spiral.
The input is the size of the matrix. Use tabs as a delimeter.

IN

3

OUT

1	2	3
8	9	4
7	6	5

IN

5

OUT

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
Login to submit solution


30 symbols pyth
Thu Nov 17 04:16:40 2016 Sait2000
Vu+]_r~-QlGQC_GytQ]]h=t*QQj\	N
33 symbols pyth
Thu Nov 17 04:09:20 2016 Sait2000
J]]hKt*QQWK=J+]_r~-KlJKC_J;VJj\	N
34 symbols pyth
Mon Oct 24 13:25:43 2016 Sait2000
J]]K*QQWtK=H~-KlJ=J+]rKHC_J;VJj\	N
36 symbols pyth
Mon Sep 19 14:35:19 2016 Sait2000
=G*QQJ]]GWtGA,-GlJG=J+]rGHC_J;VJj\	N
109 symbols python2
Mon Oct 24 09:33:47 2016 Sait2000
p=input()**2
a=[p],
while~-p:p,q=p-len(a),p;a=[range(p,q)]+zip(*a[::-1])
for b in a:print'	'.join(map(str,b))
110 symbols python2
Mon Sep 19 14:27:09 2016 Sait2000
p=input()**2
a=[[p]]
while~-p:p,q=p-len(a),p;a=[range(p,q)]+zip(*a[::-1])
for b in a:print'	'.join(map(str,b))
111 symbols python2
Mon Nov 23 04:14:10 2015 Sait2000
p=input()**2
a=[[p]]
while~-p:p,q=p-len(a),p;a=[range(p,q)]+zip(*a[::-1])
for b in a:print'\t'.join(map(str,b))
128 symbols python2
Sun Aug 10 19:08:14 2014 PFischbeck
s=input()
a=[[s*s]]
exec"a=zip(*reversed(a));m=a[0][-1];a=[range(m-len(a[0]),m)]+a;"*~-s*2
for b in a:print'\t'.join(map(str,b))
143 symbols python3
Sun Aug 10 19:12:35 2014 PFischbeck
s=int(input())
a=[[s*s]]
exec("a=list(zip(*reversed(a)));m=a[0][-1];a=[range(m-len(a[0]),m)]+a;"*~-s*2)
for b in a:print('\t'.join(map(str,b)))
156 symbols haskell
Fri Mar 13 17:19:55 2015 YoshikuniJujo
main=interact$unlines.map(init.concatMap((++"\t").show)).s.read
s 0=[]
s n=[1..n]:zipWith(\i->(++[i]))[n+1..n*2-1](reverse$map(map(+(2*n-1)).reverse).s$n-1)
178 symbols perl
Sat Jun 21 23:25:25 2014 avkhozov
#!/usr/bin/perl -nl
$n=$_;$"="\t";sub x{$m=$.;for(1..4){$r->[$x][$z]=$.++,eval'$'.($_%2?z:x).($_>2?'--':'++')while$.<$m+$_*$n-$_;$r->[$x][$z]=$.if$_<4}++$x;++$z;$n-=2;&x if$n>0}x;print"@$_"for@$r
199 symbols perl
Sat Jun 21 22:51:47 2014 avkhozov
#!/usr/bin/perl -nl
$"="\t";$v=1;sub x{($x,$z,$m,$n)=@_;for(1..4){$r->[$x][$z]=$v++,eval'$'.($_%2?z:x).($_>2?'--':'++')while$v<$m+$_*$n-$_;$r->[$x][$z]=$v if$_<4}x($x+1,$z+1,$v,$n-2)if$n>2}x 0,0,1,$_;print"@$_"for@$r
206 symbols perl
Sat Jun 21 22:39:37 2014 avkhozov
#!/usr/bin/perl -nl
$v=1;sub x{($x,$y,$m,$n)=@_;for(1..4){$r->[$x][$y]=$v++,eval'$'.($_%2?'y':'x').($_>2?'--':'++')while$v<$m+$_*($n-1);$r->[$x][$y]=$v if$_<4}x($x+1,$y+1,$v,$n-2)if$n>2}x(0,0,1,$_);print join"\t",@$_ for@$r
208 symbols perl
Sat Jun 21 22:34:38 2014 avkhozov
#!/usr/bin/perl -nl
sub x{($x,$y,$m,$n)=@_;$v=$m;for(1..4){$r->[$x][$y]=$v++,eval'$'.($_%2?'y':'x').($_>2?'--':'++')while$v<$m+$_*($n-1);$r->[$x][$y]=$v if$_!=4}x($x+1,$y+1,$v,$n-2)if$n>2}x(0,0,1,$_);print join"\t",@$_ for@$r
237 symbols perl
Sat Jun 21 21:45:16 2014 avkhozov
#!/usr/bin/perl -n
$n=$_;$o=$n*$n;@a=(0)x$o;$m=$i=1;for$p(1..$o){$a[$y*$n+$x]=$p.($x<$n-1?"\t":"\n");$x+=$i;$y+=$j;if($x<0||$y<0||$x>=$n||$y>=$n||$a[$y*$n+$x]!=0){$x-=$i;$y-=$j;$i=($i==0)*$m;$j=($j==0)*$m;if($j!=0){$m*=-1};$x+=$i;$y+=$j}};print join"",@a
239 symbols perl
Fri Jun 20 17:56:11 2014 KoukiMino
#!/usr/bin/perl -n
$n=$_;$o=$n*$n;@a=(0)x$o;$m=$i=1;for$p(1..$o){$a[$y*$n+$x]=$p.($x<$n-1?"\t":"\n");$x+=$i;$y+=$j;if($x<0||$y<0||$x>=$n||$y>=$n||$a[$y*$n+$x]!=0){$x-=$i;$y-=$j;$i=($i==0)*$m;$j=($j==0)*$m;if($j!=0){$m*=-1};$x+=$i;$y+=$j}};print join("",@a)
244 symbols perl
Fri Jun 13 19:57:42 2014 danieljabailey
#!/usr/bin/perl -n
$n=$_;$c=$_;$a=$n+1;$s=0;$l=0;for$y(0..$n-1){$c=$y>0?$c+(($a-=2)*4)-2:$c;for(0..$n-1){$r=$c-($n-($_+1)-$s);$p[$_]=$r>$c?$p[$_]+1>$c?$l-1:$p[$_]+1:($y-1>$_)&&($n-$_>$y+1)?$p[$_]-1:$r;print $p[$_].(($_+1!=$n)?"\t":"");$l=$p[$_];}print $/;$s++;}
301 symbols php
Fri Jun 20 14:17:16 2014 KoukiMino
<?php $n=fgets(STDIN);$a=array_fill(0,$n,array_fill(0,$n,0));$p=$x=$y=$j=0;$m=$i=1;while($p++<$n*$n){$a[$y][$x]=$p;$x+=$i;$y+=$j;if(!(isset($a[$y][$x]) && $a[$y][$x]===0)){$x-=$i;$y-=$j;$i!=0?$i=0:$i=$m;$j!=0?$j=0:$j=$m;if($j!=0)$m*=-1;$x+=$i;$y+=$j;};};foreach($a as $v)print implode("\t",$v)."\n";?>
314 symbols php
Wed Mar 19 04:59:08 2014 danieljabailey
<?php $n=fgets(STDIN);$c=$n;$a=$n+1;$s=0;$p=[];$l=0;for($y=0;$y<$n;$y++){$x=0;$c=$y>0?$c+(($a-=2)*4)-2:$c;for(;$x<$n;$x++){$r=$c-($n-($x+1)-$s);$p[$x]=$r>$c?$p[$x]=$p[$x]+1>$c?$l-1:$p[$x]+1:$p[$x]=($y-1>$x)&&($n-$x>$y+1)?$p[$x]-1:$r;print $p[$x]; if($x+1!=$n){print"\t";}$l=$p[$x];}if($y+1!=$n){print"\n";}$s++;}?>
342 symbols php
Wed Mar 19 02:13:30 2014 danieljabailey
<?php $n=fgets(STDIN);$c=$n;$a=$n-1;$s=0;$p=[];$l=0;for($y=0;$y<$n;$y++){$x=0;if($y>0){$c=$c+($a*4)-2;$a-=2;}for(;$x<$n;$x++){$r=$c-($n-($x+1)-$s);if($r>$c){if($p[$x]+1>$c){$p[$x]=$l-1;}else{$p[$x]=$p[$x]+1;}}else{if(($y-1>$x)&&($n-$x>$y+1)){$p[$x]=$p[$x]-1;}else{$p[$x]=$r;}}print $p[$x];if($x+1!=$n){print"\t";}$l=$p[$x];}print"\n";$s++;}?>

View all solutions