How to resolve the algorithm Magic squares of doubly even order step by step in the Ruby programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Magic squares of doubly even order step by step in the Ruby programming language
Table of Contents
Problem Statement
A magic square is an N×N square matrix whose numbers consist of consecutive numbers arranged so that the sum of each row and column, and both diagonals are equal to the same sum (which is called the magic number or magic constant).
A magic square of doubly even order has a size that is a multiple of four (e.g. 4, 8, 12).
This means that the subsquares also have an even size, which plays a role in the construction.
Create a magic square of 8 × 8.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Magic squares of doubly even order step by step in the Ruby programming language
This Ruby code defines two methods to create and display a double even magic square:
double_even_magic_square(n)
:
- It takes one argument,
n
, which must be an even multiple of 4 (e.g., 8, 12, 16, 20). - It raises an
ArgumentError
ifn
is not a multiple of 4. - It calculates various constants based on
n
:block_size
(n/4
) andmax
(n*n
). - It defines a
pre_pat
list to create a pattern that alternates between 'true' and 'false'. This pattern is used to generate a sequence of numbers that form the magic square. - It flattens and repeats the
pre_pat
pattern to create a single arraypattern
of lengthn*n
. - It creates a flat array
flat_ar
by iterating throughpattern
and converting 'true' elements to numbers ranging from 1 tomax
and 'false' elements to numbers ranging frommax
to 1. - It slices the
flat_ar
into rows of sizen
and returns a 2D array representing the magic square.
to_string(square)
:
- It takes one argument,
square
, which is a 2D array representing the magic square. - It calculates the number of columns
n
as the square's length. - It defines a format string
fmt
to format the numbers with the correct width based on the number of digits in the largest number in the square. - It iterates through the
square
, flattening each row and inserting the formatted row into a string. - It returns the resulting string.
In the main block of the code:
- It calls
double_even_magic_square(8)
to create an 8x8 double even magic square. - It calls
to_string
to format and display the magic square as a string.
Source code in the ruby programming language
def double_even_magic_square(n)
raise ArgumentError, "Need multiple of four" if n%4 > 0
block_size, max = n/4, n*n
pre_pat = [true, false, false, true,
false, true, true, false]
pre_pat += pre_pat.reverse
pattern = pre_pat.flat_map{|b| [b] * block_size} * block_size
flat_ar = pattern.each_with_index.map{|yes, num| yes ? num+1 : max-num}
flat_ar.each_slice(n).to_a
end
def to_string(square)
n = square.size
fmt = "%#{(n*n).to_s.size + 1}d" * n
square.inject(""){|str,row| str << fmt % row << "\n"}
end
puts to_string(double_even_magic_square(8))
You may also check:How to resolve the algorithm Show the epoch step by step in the Dart programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the DWScript programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the Python programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Go programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the PowerBASIC programming language