On 5/18/07, Ruby Quiz <james / grayproductions.net> wrote: > > This week's Ruby Quiz is to write a program that builds magic squares. To keep > the problem easy, I will say that your program only needs to work for odd values > of N. Try to keep your runtimes pretty reasonable even for the bigger values of > N: > # I know that people are already working on the next Ruby Quiz, # but this idea just hit me and I made this improvement to my solution. # If I make more improvements, I won't bother everyone with more posts. # I just thought this quiz was interesting. ### Code Start num = ARGV[0].to_i if num % 2 != 0 and num > 0 tot = (0...num).map {Array.new(num)} (1..num**2).each do |x| tot.unshift(tot.pop).each {|g| g.push(g.shift)} if x % num != 1 tot.push(tot.shift) if x % num == 1 tot[0][((num + 1) / 2) - 1] = x.to_s.rjust((num**2).to_s.length) end tot.push(tot.shift) tot.each {|x| p x.join(" ")} end ### # Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/