in reply to (golf) Triangle of numbers

Conditions state we assume the # of rows is in $a, so not counting the initial assignment, 41 characters:
$a=5; print map{@_=$j+1..($j+=++$i);"@_\n"}1..$a #Update 2 chars better (39) print map{"@{[$j+1..($j+=++$i)]}\n"}1..$a
Update: blakem pointed out that mine doesn't align the numbers. Darn.
Another update: I don't see any reason why any one of the following does not work, there seems to be a bug with the deprecated '$#' variable. In fact, the one with 's' segfaults on 5.6.1 (though if it worked, it would left justify the numbers). It woulda been 64 characters (if someone submits a patch to the perl core, they can have the credit for this :)
$#="%@{[2*length $a]}d";print map"@{[$j+1..($j+=++$i)]}\n",1..$a $#="%@{[2*length $a]}u";print map"@{[$j+1..($j+=++$i)]}\n",1..$a $#="%@{[2*length $a]}s";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
Last Update: Thanks to help from master caddy Albannach (for figuring out that 'f' works), 60 characters:
$#=" %@{[2*length$a]}.f";print map{$j+1..($j+=++$i),$/}1..$a
Really last update: Following the letter AND spirit of that last 'updated' condition, at 67 characters:
$#=" %@{[length int$a*$a/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a # or $#=" %@{[length$a++*$a/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a-1 # or $#=" %@{[length$a*($a+1)/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a
Tired of updating :) But more caddy help, this time from blakem(65 characters):
$#=" %@{[length$a++*$a/2]}g";map{print$j+1..($j+=++$i),$/}1..$a-1

Replies are listed 'Best First'.
Re: Re: (golf) Triangle of numbers
by demerphq (Chancellor) on Oct 27, 2001 at 22:48 UTC
    Based on a few ideas from the other posts I came up with the following improvement of your 65 character version, squeezing it to 62. (Now all I have to go and read the docs and figure out how it works. :-) The one I was working on (and understand) was in the line of hopes latest post which I reduced down to 64.
    1 2 3 4 5 6 12345678901234567890123456789012345678901234567890123456789012345 #runrig improvement $#=" %@{[length$a++*$a/2]}g";print$j+1..($j+=++$i),$/for 2..$a #blakem/hopes/jynx improvement $;=length$a++*$a/2;map{printf" %$;d",++$,for 2..$_;print$/}2..$a
    Ahhh, Latecomers Advantage. :-)

    Yves / DeMerphq
    --
    Have you registered your Name Space?