Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Diamonds for fun

by ambrus (Abbot)
on Mar 07, 2011 at 14:13 UTC ( #891806=note: print w/replies, xml ) Need Help??

in reply to Diamonds for fun

I'm quite sure this can be golfed further, but as it seems shorter than the above solutions I must post it.

perl -le 'printf"%*s\n",$t+$_,(1x$_)**2for(1x($t=pop))**2=~/./g' 9

Update: see also Christmas Tree.

Update: one character shorter:

perl -e 'printf"%*s\n",$t+$_,(1x$_)**2for(1x($t=pop))**2=~/./g' 9

Somewhat shorter:

perl -le 'print" "x($t-$_),(1x$_)**2for(1x($t=pop))**2=~/./g' 9

Update 2012-03-14: and see Spiraling integers for yet another arrangement of numbers.

Replies are listed 'Best First'.
Re^2: Diamonds for fun
by toolic (Bishop) on Mar 07, 2011 at 14:45 UTC
    You can shave 3 off your lowest score using perl 5.10:
    # score = 53 # 1 2 3 4 5 # 12345678901234567890123456789012345678901234567890123456789 perl -E'say" "x($t-$_),(1x$_)**2for(1x($t=pop))**2=~/./g' 9
    update due to hbm's observation:
    # score = 52 # 1 2 3 4 5 # 12345678901234567890123456789012345678901234567890123456789 perl -E'say$"x($t-$_),(1x$_)**2for(1x($t=pop))**2=~/./g' 9
Re^2: Diamonds for fun
by hbm (Hermit) on Mar 07, 2011 at 20:38 UTC

    Squaring a string of ones ((1x$_)**2) is very clever! This works for me up to 8, but 9 fails (because 111111111**2 = 1.23456789876543e+16). (Same for toolic's solution below...)

    You can save one more character with $" instead of " ".

      It works for 9 on a 64 bit system, which is why I didn't notice it's broken, but it is indeed broken on 64 bit systems.

      You could try to fix it with something like this, but as is that's too long and I don't feel much like golfing it now.

      perl -e 'printf"%*.0f\n",$t+abs,(1x abs)**2for 1..($t=pop),1-$t..-1' 9

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://891806]
and the radiator hisses contentedly...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-03-28 06:38 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (327 votes). Check out past polls.