# Please Forward: Ruby Quiz Submission

**URL:** <https://rubytalk.org/t/please-forward-ruby-quiz-submission/37719>\
**Category:** ruby-talk\
**Created:** [20 May 2007 15:11 UTC](https://rubytalk.org/t/please-forward-ruby-quiz-submission/37719 "2007-05-20T15:11:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [20 May 2007 15:11 UTC](https://rubytalk.org/t/please-forward-ruby-quiz-submission/37719/1 "2007-05-20T15:11:32Z")

</div>

> From: James Koppel \<darmaniiii@yahoo.com\>  
> Date: May 18, 2007 1:35:48 PM CDT  
> To: submission@rubyquiz.com, submission@rubyquiz.com  
> Subject: Please Forward: Ruby Quiz Submission
> 
> Here is my solution to Ruby Quiz 124. It does not implement the extra credit. It uses the following algorithm (courtesy of Wikipedia):
> 
> "Starting from the central column of the first row with the number 1, the fundamental movement for filling the squares is diagonally up and right, one step at a time. If a filled square is encountered, one moves vertically down one square instead, then continuing as before. When a move would leave the square, it is wrapped around to the last row or first column, respectively."
> 
> def magic\_square(n)  
> &nbsp;&nbsp;square = .fill(nil,0...n).map{  
> &nbsp;&nbsp;&nbsp;&nbsp;.fill(nil,0...n)}  
> &nbsp;&nbsp;x,y = (n-1)/2, 0  
> &nbsp;&nbsp;nxt = 1  
> &nbsp;&nbsp;while nxt \<= n\*\*2  
> &nbsp;&nbsp;&nbsp;&nbsp;square[y] = nxt  
> &nbsp;&nbsp;&nbsp;&nbsp;nxt += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;if square[(y-n-1)%n][(x+n+1)%n]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x,y, = (x+n+1)%n,(y-n-1)%n  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;square  
> end
> 
> n = ARGV[0].to\_i  
> digits = (n\*\*2).to\_s.length  
> square = magic\_square(n)  
> square.map! {|arr| arr.map{|x|" " \* (digits - x.to\_s.length) + "#{x}"}}  
> n.times {|t|  
> &nbsp;&nbsp;puts "+" + ("-" \* (n\*(digits + 3)-1)) + "+"  
> &nbsp;&nbsp;puts "| #{square[t].join(' | ')} |"}  
> puts "+" + ("-" \* (n\*(digits + 3)-1)) + "+"
> 
> Pinpoint customers who are looking for what you sell.

> **···**
>
> Begin forwarded message:
