Questions tagged [magic-square]

A "magic square" is an arrangement of numbers (usually integers) in a square grid, where the numbers in each row, and in each column, and the numbers in the forward and backward main diagonals, all add up to the same number.

In recreational mathematics, a magic square is an arrangement of numbers (usually integers) in the form of a square grid, where the numbers in each row, and in each column, and the numbers in the forward and backward main diagonals, all add up to the same number.

A magic square has the same number of rows as it has columns, and in conventional math notation, "n" stands for the number of rows (and columns) it has.

Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being "of order n". A magic square that contains the integers from 1 to n2 is called a normal magic square. (The term "magic square" is also sometimes used to refer to any of various types of word squares.)

Wikipedia:
http://en.wikipedia.org/wiki/Magic_square

148 questions
11
votes
3 answers

NumPy equivalent of Matlab's magic()

In Ocatave / Matlab, I can use magic() to get a magic square, e.g., magic(4) 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 Definition: A magic square is an N×N grid of numbers in which the entries in each row,…
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
10
votes
2 answers

Too much memory consumption when generating magic squares in erlang - Need help for optimization

for university I have to implement an algorithm which creates all possibile magic squares for a given edge length and a specific sum. For n=3 the algorithm is working as expected. But when generating all magic squares for n=4 after a while I ran out…
soupdiver
  • 3,504
  • 9
  • 40
  • 68
6
votes
5 answers

Magic Square Program (C++)

For those unfamiliar with the classic magic square algorithm: A magic square is a two dimensional array (n x n) which contains a numerical value between the values 1 and n^2 in each location. Each value may appear only once. Furthermore, the sum of…
user533053
  • 79
  • 1
  • 1
  • 3
5
votes
5 answers

Puzzle Programming - Impossible to optimize?

I've been writing programs to solve various number puzzles, but I'm constantly designing unreasonably complex search algorithms that I can't optimize. For example, in one puzzle, you are given a 3x3 grid of numbers 1 through 9…
xikkub
  • 1,641
  • 1
  • 16
  • 28
5
votes
2 answers

How to create a symmetric matrix of 1's and 0's with constant row and column sum

I'm trying to find an elegant algorithm for creating an N x N matrix of 1's and 0's, under the restrictions: each row and each column must sum to Q (to be picked freely) the diagonal must be 0's the matrix must be symmetrical. It is not strictly…
Kaare
  • 531
  • 1
  • 7
  • 26
4
votes
5 answers

What is the optimal way of generating all possible 3X3 magic squares?

Magic square: sum of any row, column, or diagonal of length is always equal to the same number. All 9 numbers are distinct positive integers. I am doing it this way in JavaScript, but what is the optimal way of generating all of them? function…
user7331530
  • 815
  • 1
  • 12
  • 21
4
votes
2 answers

Java Magic Square - Sum Column and Sum Row Errors

I've been assigned a school assignment to make and check for a 'Magic Square' in a 2d array generated by the user in a N*N matrix. So far, I have gotten most of the code right (I have tested each method individually). However, I am unable to correct…
user5283917
3
votes
2 answers

Solving the Magic Square with Genetic Algorithms: population score converges fast, but never reaches the goal?

The desire to learn more about GA has sparkled again, and instead of reading a lot and doing nothing, I've decided to start the other way around: pick a problem and try to solve it. I've picked the Magic Square problem. For encoding the…
Mike Moe
  • 226
  • 2
  • 5
3
votes
5 answers

Brute force magic squares

Basically I have a 3 x 3 grid that is filled with two digit numbers 00 - 99. Some of the numbers are given as input the rest are unknown. What are some suggestions on how to solve such a problem with brute force in C? EDIT: Sorry I forgot part of…
foo
  • 2,241
  • 6
  • 21
  • 26
3
votes
2 answers

How to change an ArrayList into a 2D array in Java

I am supposed to be making a program that tests a user inputed matrix is a magic square. Basically I should be putting user input into an ArrayList which is then placed into a 2D array that can then be used to calculate the sum of the rows, col, and…
3
votes
1 answer

Generate a 3x3 Magic Square

The 3x3 magic square I'm working with is filled with numbers 1-9. The values in each row, column, and diagonal must add to 15. I must follow this pseudocode: recursive_funtion(position) { for number from 1 to 9, not used elsewhere already { …
CS student
  • 305
  • 1
  • 3
  • 13
3
votes
3 answers

How to create a magic square in PHP?

I'd like to try my hand at creating a Magic Square in PHP (i.e. a grid of numbers that all add up to the same value), but I really don't know where to start. I know of the many methods that create magic square, such as starting "1" at a fixed…
TerranRich
  • 1,263
  • 3
  • 20
  • 38
3
votes
1 answer

function to find out how many magic squares are in rectangle made of n*m, where n,m - natural numbers

I need to write an algorithm how i would solve this exercise, any suggestions? Exercise: We have a rectangle, divided into n x m squares, with natural numbers. Write a function that counts how many magic squares are inside this rectangle. A magic…
TechnoA
  • 75
  • 1
  • 10
2
votes
1 answer

Prolog and Magic Squares with CLPFD

I'm stuck getting a generalized version of a "Magic Square" program in SWI-prolog to work. This is my approach: :- use_module(library(clpfd)). sumok(Sum, Lst) :- sum_list(Lst, Sum). solve(Size, Grid) :- length(Grid, Size),…
Hennes
  • 1,340
  • 1
  • 10
  • 26
2
votes
2 answers

Check if my matrix is a magic square javascript

We have been given an assignment to create a 3x3 matrix using input boxes and make a code to check if the given numbers add up correctly to make is a magic square. No prompt boxes and for us to use an array to accomplish the task. I have used input…
Ellis Jordan
  • 59
  • 2
  • 6
1
2 3
9 10