s

Code Golf: Numeric Diamonds

Dec 08, 2007

Here is my submission for the Numeric Diamonds challenge. Code size: 219 bytes.

[9,36,100].each{|n|s=Math.sqrt(n).to_i
w=n.to_s.size.to_i
a=[]
j=s+s-1
j.times{a<<[" "*w]*j}
x=0
y=s-1
f=x
g=y
1.upto(n){|t|a[x][y]=t.to_s.rjust w
t%s<1 ?(x=f+1;f=x;y=g-1;g=y):(x+=1;y+=1)}
a.each{|r|puts r.join.rstrip}}

Tagged: rubycode golf