From: "G.Durga Prasad" Date: 2007-05-23T00:21:30+09:00 Subject: Re: [QUIZ] Magic Squares (#124) ------=_Part_54135_32723785.1179847290951 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline $ cat magic_square.rb #!/usr/bin/env ruby # G.D.Prasad class Array def / len # Thanks to _why a=[] each_with_index do |x,i| a <<[] if i%len == 0 a.last << x end a end def rotate_left self.push(self.shift) end def rotate_right self.unshift(self.pop) end end def rotate_array_right_index_times(arrays) arrays.each_with_index{|array,i| i.times{array = array.rotate_right}} end def show(rows,n) string = rows.map{|r| r.inject(""){|s,e| s + e.to_s.center(5," ") +"|"}}.join("\n"+"-"*6*n+"\n") puts string end n=ARGV[0].to_i raise "Usage: magic_square (ODD_NUMBER>3) " if n%2==0 or n<3 nsq=n*n arrays = ((1..nsq).to_a/n).each{|a| a.reverse!} sum = nsq*(nsq+1)/(2*n) (n/2).times{arrays = arrays.rotate_left} rotate_array_right_index_times(arrays) cols=arrays.transpose rotate_array_right_index_times(cols) rows=cols.transpose puts;puts show(rows,n) puts puts " sum of each row,column or diagonal = #{sum}" puts;puts On 5/18/07, Ruby Quiz wrote: > The three rules of Ruby Quiz: > > 1. Please do not post any solutions or spoiler discussion for this quiz until > 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz by submitting ideas as often as you can: > > http://www.rubyquiz.com/ > > 3. Enjoy! > > Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone > on Ruby Talk follow the discussion. Please reply to the original quiz message, > if you can. > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > A magic square of size N is a square with the numbers from 1 to N ** 2 arranged > so that each row, column, and the two long diagonals have the same sum. For > example, a magic square for N = 5 could be: > > +------------------------+ > | 15 | 8 | 1 | 24 | 17 | > +------------------------+ > | 16 | 14 | 7 | 5 | 23 | > +------------------------+ > | 22 | 20 | 13 | 6 | 4 | > +------------------------+ > | 3 | 21 | 19 | 12 | 10 | > +------------------------+ > | 9 | 2 | 25 | 18 | 11 | > +------------------------+ > > In this case the magic sum is 65. All rows, columns, and both diagonals add up > to that. > > 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: > > $ time ruby magic_square.rb 9 > +--------------------------------------------+ > | 45 | 34 | 23 | 12 | 1 | 80 | 69 | 58 | 47 | > +--------------------------------------------+ > | 46 | 44 | 33 | 22 | 11 | 9 | 79 | 68 | 57 | > +--------------------------------------------+ > | 56 | 54 | 43 | 32 | 21 | 10 | 8 | 78 | 67 | > +--------------------------------------------+ > | 66 | 55 | 53 | 42 | 31 | 20 | 18 | 7 | 77 | > +--------------------------------------------+ > | 76 | 65 | 63 | 52 | 41 | 30 | 19 | 17 | 6 | > +--------------------------------------------+ > | 5 | 75 | 64 | 62 | 51 | 40 | 29 | 27 | 16 | > +--------------------------------------------+ > | 15 | 4 | 74 | 72 | 61 | 50 | 39 | 28 | 26 | > +--------------------------------------------+ > | 25 | 14 | 3 | 73 | 71 | 60 | 49 | 38 | 36 | > +--------------------------------------------+ > | 35 | 24 | 13 | 2 | 81 | 70 | 59 | 48 | 37 | > +--------------------------------------------+ > > real 0m0.012s > user 0m0.006s > sys 0m0.006s > > For extra credit, support even values of N. You don't need to worry about N = 2 > though as it is impossible. > > ------=_Part_54135_32723785.1179847290951 Content-Type: application/x-ruby; name="magic_square.rb" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="magic_square.rb" X-Attachment-Id: f_f20ill32 IyEvdXNyL2Jpbi9lbnYgcnVieSAKI0cuRC5QcmFzYWQKY2xhc3MgQXJyYXkKICBkZWYgLyBsZW4g ICAgICAgICAgICMgVGhhbmtzIHRvIF93aHkKICBhPVtdCiAgZWFjaF93aXRoX2luZGV4IGRvIHx4 LGl8CiAgYSA8PFtdIGlmIGklbGVuID09IDAKICBhLmxhc3QgPDwgeAogIGVuZAogIGEKICBlbmQK ICBkZWYgcm90YXRlX2xlZnQKICAgIHNlbGYucHVzaChzZWxmLnNoaWZ0KQogIGVuZCAgCiAgZGVm IHJvdGF0ZV9yaWdodAogICAgc2VsZi51bnNoaWZ0KHNlbGYucG9wKQogIGVuZCAgCmVuZAoKZGVm IHJvdGF0ZV9hcnJheV9yaWdodF9pbmRleF90aW1lcyhhcnJheXMpCiAgYXJyYXlzLmVhY2hfd2l0 aF9pbmRleHt8YXJyYXksaXwgaS50aW1lc3thcnJheSA9IGFycmF5LnJvdGF0ZV9yaWdodH19CmVu ZAoKZGVmIHNob3cocm93cyxuKQogIHN0cmluZyAgPSByb3dzLm1hcHt8cnwgci5pbmplY3QoIiIp e3xzLGV8IHMgKyBlLnRvX3MuY2VudGVyKDUsIiAiKSArInwifX0uam9pbigiXG4iKyItIio2Km4r IlxuIikKICBwdXRzIHN0cmluZwplbmQKCm49QVJHVlswXS50b19pCnJhaXNlICJVc2FnZTogbWFn aWNfc3F1YXJlIChPRERfTlVNQkVSPjMpICIgaWYgbiUyPT0wIG9yIG48Mwpuc3E9bipuCmFycmF5 cyA9ICgoMS4ubnNxKS50b19hL24pLmVhY2h7fGF8IGEucmV2ZXJzZSF9CnN1bSA9IG5zcSoobnNx KzEpLygyKm4pCihuLzIpLnRpbWVze2FycmF5cyA9IGFycmF5cy5yb3RhdGVfbGVmdH0Kcm90YXRl X2FycmF5X3JpZ2h0X2luZGV4X3RpbWVzKGFycmF5cykKY29scz1hcnJheXMudHJhbnNwb3NlCnJv dGF0ZV9hcnJheV9yaWdodF9pbmRleF90aW1lcyhjb2xzKQpyb3dzPWNvbHMudHJhbnNwb3NlCgpw dXRzO3B1dHMKc2hvdyhyb3dzLG4pCnB1dHMKcHV0cyAiCXN1bSBvZiBlYWNoIHJvdyxjb2x1bW4g b3IgZGlhZ29uYWwgPSAgI3tzdW19IgpwdXRzO3B1dHMK ------=_Part_54135_32723785.1179847290951--