[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] Magic Squares (#124

email55555 email55555

5/21/2007 1:28:00 AM

#---------------------------------------------------------------#
# #
# Program : Magic Square #
# Author : David Tran #
# Date : 2007-05-20 #
# Blog : http://davidtran.doublegifts.com/... #
# Reference : http://mathworld.wolfram.com/MagicS... #
# #
#---------------------------------------------------------------#
class MagicSquare

def initialize(size = 3)
raise "Error: size must greater than 2." if size < 3
@magic_square = if (size % 2 != 0)
OddMagicSquare.new(size)
elsif (size % 4 == 0)
DoublyEvenMagicSquare.new(size)
else
SinglyMagicSquare.new(size)
end
end

def size
@magic_square.size
end

def [](i,j)
@magic_square[i,j]
end

def to_s
digits = (size * size).to_s.size
divider = '+' + '-' * ((digits + 2) * size + (size - 1)) + "+\n"
(0...size).inject(divider) do |s, i|
(0...size).inject(s + "|") do |s, j|
s + " #{self[i,j].to_s.rjust(digits)} |"
end + "\n" + divider
end
end

def is_magic_square?
sum = size * (size * size + 1) / 2
(0...size).each do |i|
return false if sum != (0...size).inject(0) { |s,j| s + self[i,j] }
return false if sum != (0...size).inject(0) { |s,j| s + self[j,i] }
end
return false if sum != (0...size).inject(0) { |s,i| s + self[i,i] }
return false if sum != (0...size).inject(0) { |s,i| s + self[i, size-1-i] }
true
end

private
#------------------------------------------------------------------#
class OddMagicSquare
attr_reader :size

def initialize(size)
@size = size
n = @size * @size
@array = Array.new(n)
i, j = 0, @size/2
(1..n).each do |v|
@array[get_index(i,j)] = v
a, b = i-1, j+1
i, j = self[a,b] ? [i+1, j] : [a, b]
end
end

def [](i, j)
@array[get_index(i,j)]
end

private
def get_index(i, j)
(i % @size) * @size + (j % @size)
end
end
#------------------------------------------------------------------#
class DoublyEvenMagicSquare
attr_reader :size

def initialize(size)
@size = size
end

def [](i, j)
value = (i * @size) + j + 1
i = (i % @size) % 4
j = (j % @size) % 4
((i == j) || (i + j == 3)) ? (@size*@size+1-value) : value
end
end
#------------------------------------------------------------------#
class SinglyMagicSquare
attr_reader :size

def initialize(size)
@size = size
@odd_magic_square = MagicSquare.new(@size/2)
end

def [](i, j)
i, j = i % @size, j % @size
ii, jj = i / 2, j / 2
center = @size / 2 / 2
value = @odd_magic_square[ii, jj]
_L = [4, 1, 2, 3]
_U = [1, 4, 2, 3]
_X = [1, 4, 3, 2]
if jj == center
if ii < center then _L
elsif ii == center then _U
elsif ii == center+1 then _L
else _X
end
else
if ii <= center then _L
elsif ii == center+1 then _U
else _X
end
end.map {|e| 4 * (value - 1) + e} [i%2*2 + j%2]
end
end
#------------------------------------------------------------------#
end

if __FILE__ == $0
puts MagicSquare.new(ARGV[0].to_i)
end

8 Answers

email55555 email55555

5/21/2007 2:18:00 AM

0

Updated.

#---------------------------------------------------------------#
# #
# Program : Magic Square #
# Author : David Tran #
# Date : 2007-05-20 #
# Blog : http://davidtran.doublegifts.com/... #
# Reference : http://mathworld.wolfram.com/MagicS... #
# #
#---------------------------------------------------------------#
class MagicSquare

def initialize(size = 3)
raise "Error: size must greater than 2." if size < 3
@magic_square = if (size % 2 != 0)
OddMagicSquare.new(size)
elsif (size % 4 == 0)
DoublyEvenMagicSquare.new(size)
else
SinglyEvenMagicSquare.new(size)
end
end

def size
@magic_square.size
end

def [](i,j)
@magic_square[i,j]
end

def to_s
digits = (size * size).to_s.size
divider = '+' + '-' * ((digits + 2) * size + (size - 1)) + "+\n"
(0...size).inject(divider) do |s, i|
(0...size).inject(s + "|") do |s, j|
"#{s} #{self[i,j].to_s.rjust(digits)} |"
end + "\n" + divider
end
end

def is_magic_square?
sum = size * (size * size + 1) / 2
(0...size).each do |i|
return false if sum != (0...size).inject(0) { |s,j| s + self[i,j] }
return false if sum != (0...size).inject(0) { |s,j| s + self[j,i] }
end
return false if sum != (0...size).inject(0) { |s,i| s + self[i,i] }
return false if sum != (0...size).inject(0) { |s,i| s + self[i, size-1-i] }
true
end

private
#------------------------------------------------------------------#
class OddMagicSquare
attr_reader :size

def initialize(size)
@size = size
n = @size * @size
@array = Array.new(n)
i, j = 0, @size/2
(1..n).each do |v|
@array[get_index(i,j)] = v
a, b = i-1, j+1
i, j = self[a,b] ? [i+1, j] : [a, b]
end
end

def [](i, j)
@array[get_index(i,j)]
end

private
def get_index(i, j)
(i % @size) * @size + (j % @size)
end
end
#------------------------------------------------------------------#
class DoublyEvenMagicSquare
attr_reader :size

def initialize(size)
@size = size
end

def [](i, j)
value = (i * @size) + j + 1
i = (i % @size) % 4
j = (j % @size) % 4
((i == j) || (i + j == 3)) ? (@size*@size+1-value) : value
end
end
#------------------------------------------------------------------#
class SinglyEvenMagicSquare
attr_reader :size

def initialize(size)
@size = size
@odd_magic_square = MagicSquare.new(@size/2)
end

def [](i, j)
i, j = i % @size, j % @size
ii, jj = i / 2, j / 2
center = @size / 2 / 2
value = @odd_magic_square[ii, jj]
_L = [4, 1, 2, 3]
_U = [1, 4, 2, 3]
_X = [1, 4, 3, 2]
if jj == center
if ii < center then _L
elsif ii == center then _U
elsif ii == center+1 then _L
else _X
end
else
if ii <= center then _L
elsif ii == center+1 then _U
else _X
end
end [i%2*2 + j%2] + 4 * (value - 1)
end
end
#------------------------------------------------------------------#
end

if __FILE__ == $0
puts MagicSquare.new(ARGV[0].to_i)
end

email55555 email55555

5/21/2007 1:05:00 PM

0

Hi,

Updated again on SinglyEvenMagicSquare class. ( shorter )
Basically, it almost remains the same.

Don't like to post too many emails when I find/try to improve my
program on the details ...
If you read my previous 2 post solution, and like to see the "little"
change, please go to my blog: ( it will have my last updated version )

http://davidtran.doublegifts.com/...

otherwise, I am sorry to bother you on the mailing list.

regards,
David Tran

doug meyer

5/21/2007 2:27:00 PM

0

Hey,
Did anyone else run into the problem of trying to setup your square using:
Array.new(size, Array.new(size))
that was annoying! And I wasted too much time with formating the output.

http://pastie.cabo...

#!/usr/bin/env ruby
# Douglas Meyer

class Array
def sum
inject(0){|a,v|a+=v}
end
end

rows = ARGV[0].to_i
puts "Sorry, this program only works for odd row values!" if rows % 2 == 0

row_sum = (1..rows**2).to_a.sum/rows
puts "Row Sum: #{row_sum}"
decimals = (Math::log(row_sum)/Math::log(10)).to_i

square = Array.new(rows){Array.new(rows, 0)}

row = (rand*rows+1).to_i
col = (rand*rows+1).to_i

#Computer square
(1..rows**2).each do |count|
row = (row - 1) % rows
col = (col - 1) % rows
square[row][col] = count
unless square[(row-1)%rows][(col-1)%rows] == 0
col -= 2
row -= 1
end
end

#Display and check sums
string = "% #{decimals+2}d"*rows+" = % #{decimals+1}d"
square.each{ |row|
output = row
output << row.sum
puts string % output
}
puts (" "*(decimals)+"= ")*rows
string = "% #{decimals+2}d"*rows
puts string % square.inject(Array.new(rows,0)){|sum,row|
sum = sum.zip(row).map{|a|a.sum}
}

On 5/21/07, David Tran <email55555@gmail.com> wrote:
> Hi,
>
> Updated again on SinglyEvenMagicSquare class. ( shorter )
> Basically, it almost remains the same.
>
> Don't like to post too many emails when I find/try to improve my
> program on the details ...
> If you read my previous 2 post solution, and like to see the "little"
> change, please go to my blog: ( it will have my last updated version )
>
> http://davidtran.doublegifts.com/...
>
> otherwise, I am sorry to bother you on the mailing list.
>
> regards,
> David Tran
>
>

Yossef Mendelssohn

5/21/2007 6:58:00 PM

0

On May 21, 9:26 am, "doug meyer" <doug.me...@sigeps.org> wrote:
> Hey,
> Did anyone else run into the problem of trying to setup your square using:
> Array.new(size, Array.new(size))
> that was annoying! And I wasted too much time with formating the output.

If you're talking about the problem I think you're talking about,
that's the reason I dismissed that approach out of hand.

irb(main):001:0> x = Array.new(5, Array.new(5,0))
=> [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0,
0], [0, 0, 0, 0, 0]]
irb(main):002:0> x[2][2] = 5
=> 5
irb(main):003:0> x
=> [[0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0,
0], [0, 0, 5, 0, 0]]

Those references will get you if you're not careful.

--
-yossef


James Gray

5/21/2007 7:02:00 PM

0

On May 21, 2007, at 1:58 PM, Yossef Mendelssohn wrote:

> On May 21, 9:26 am, "doug meyer" <doug.me...@sigeps.org> wrote:
>> Hey,
>> Did anyone else run into the problem of trying to setup your
>> square using:
>> Array.new(size, Array.new(size))
>> that was annoying! And I wasted too much time with formating the
>> output.
>
> If you're talking about the problem I think you're talking about,
> that's the reason I dismissed that approach out of hand.
>
> irb(main):001:0> x = Array.new(5, Array.new(5,0))
> => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0,
> 0], [0, 0, 0, 0, 0]]
> irb(main):002:0> x[2][2] = 5
> => 5
> irb(main):003:0> x
> => [[0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0,
> 0], [0, 0, 5, 0, 0]]
>
> Those references will get you if you're not careful.

But it's very easy to fix:

=> [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]
>> square[2][2] = 5
=> 5
>> square
=> [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
5, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]

James Edward Gray II

doug meyer

5/21/2007 8:19:00 PM

0

I assume you meant to include this:
square = Array.new(rows){Array.new(rows)}

On 5/21/07, James Edward Gray II <james@grayproductions.net> wrote:
> On May 21, 2007, at 1:58 PM, Yossef Mendelssohn wrote:
>
> > On May 21, 9:26 am, "doug meyer" <doug.me...@sigeps.org> wrote:
> >> Hey,
> >> Did anyone else run into the problem of trying to setup your
> >> square using:
> >> Array.new(size, Array.new(size))
> >> that was annoying! And I wasted too much time with formating the
> >> output.
> >
> > If you're talking about the problem I think you're talking about,
> > that's the reason I dismissed that approach out of hand.
> >
> > irb(main):001:0> x = Array.new(5, Array.new(5,0))
> > => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0,
> > 0], [0, 0, 0, 0, 0]]
> > irb(main):002:0> x[2][2] = 5
> > => 5
> > irb(main):003:0> x
> > => [[0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0,
> > 0], [0, 0, 5, 0, 0]]
> >
> > Those references will get you if you're not careful.
>
> But it's very easy to fix:
>
> => [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
> nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]
> >> square[2][2] = 5
> => 5
> >> square
> => [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
> 5, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]
>
> James Edward Gray II
>
>

Rick DeNatale

5/21/2007 8:27:00 PM

0

On 5/21/07, James Edward Gray II <james@grayproductions.net> wrote:
> On May 21, 2007, at 1:58 PM, Yossef Mendelssohn wrote:
>
> > On May 21, 9:26 am, "doug meyer" <doug.me...@sigeps.org> wrote:
> >> Hey,
> >> Did anyone else run into the problem of trying to setup your
> >> square using:
> >> Array.new(size, Array.new(size))
> >> that was annoying! And I wasted too much time with formating the
> >> output.
> >
> > If you're talking about the problem I think you're talking about,
> > that's the reason I dismissed that approach out of hand.
> >
> > irb(main):001:0> x = Array.new(5, Array.new(5,0))
> > => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0,
> > 0], [0, 0, 0, 0, 0]]
> > irb(main):002:0> x[2][2] = 5
> > => 5
> > irb(main):003:0> x
> > => [[0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0,
> > 0], [0, 0, 5, 0, 0]]
> >
> > Those references will get you if you're not careful.
>
> But it's very easy to fix:
>
I think you left out something like:

square = Array.new(5) {Array.new(5)}

or one of the other solutions.

> => [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
> nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]
> >> square[2][2] = 5
> => 5
> >> square
> => [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
> 5, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]

Are we playing tag team today, James? <G>


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

James Gray

5/21/2007 8:53:00 PM

0

On May 21, 2007, at 3:18 PM, doug meyer wrote:

> I assume you meant to include this:
> square = Array.new(rows){Array.new(rows)}

Oops, yes, I snipped it. Thanks for the correction.

James Edward Gray II