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
1
vote
0 answers

How to eliminate magic squares formed by the same numbers, but permuted

The magic squares that are formed with the first 9 numbers are: [[6, 1, 8], [7, 5, 3], [2, 9, 4]] [[2, 7, 6], [9, 5, 1], [4, 3, 8]] etc etc (8 magic squares with permutations) I would like to eliminate the permutations, so that it is considered a…
marilu71
  • 11
  • 3
1
vote
1 answer

Solving Singly Even Magic Square (Lux)

Hey guys I am trying to implement a method for solving singly even magic square, but it seems to generating the wrong results. Result generated by the following code Enter in size of square: 10 75 75 0 0 0 50 50 50 50 25 75 75 0 0 0 50 50 50…
ferronrsmith
  • 1,110
  • 5
  • 28
  • 47
1
vote
2 answers

Magic square in python

I'm new to coding. I'm trying to program a magic square. A magic square is a square (3 × 3 for my case, could be different), where all rows and columns and diagonals must sum to a certain number (15 for my case, because of the 3 × 3). Here is my…
bucephalusII
  • 59
  • 1
  • 4
1
vote
2 answers

How to perform Partial-mapped crossover in python3?

I am new to genetic algorithms and made one the other day that recreated a target string. So I tried to make one that could make a Magic Square. It was ok until I got to the crossover part, realising I couldn't just do a single point crossover. So I…
Sam
  • 65
  • 4
  • 7
1
vote
1 answer

Trouble optimising magic square generation

I'm still fairly new to programming and am currently attempting to generate a magic square based on the users input. I have managed to create some sort of abomination that works fine for a 3x3 square. The problem I'm having is that because I…
Jack Minney
  • 61
  • 1
  • 8
1
vote
2 answers

MAgic Square function C++

This is my last function for my magic square and for some reason it's giving me an error that there is "'[int]' for array subscript" but I don't know what that means, if someone could help explain what I have to do. bool Square::is_magic() { …
Richard
  • 41
  • 5
1
vote
2 answers

Printf for a Magic Square

so this is the code I have below: public void printSquare() { for (int row = 0; row < square.length; row++) { for (int col = 0; col < square.length; col++) { System.out.printf(square[row][col] + "%-3c", ' '); } …
Ryukaizan
  • 13
  • 4
1
vote
3 answers

Why is my program ending after the first for loop?

I'm writing a program that will calculate a magic square but I cant get the input to work right. I'm pretty sure I am filling the array with values but it ends right after the first two for loops. The second two are supposed to print the array but…
bills
  • 59
  • 9
1
vote
0 answers

List Index out of Range Error Whilst Making a Square

So I'm trying to make a function that takes an integer 'n' and returns a list of lists, and this is all in the vein of making a 'magic square' (start at 1 in the top center, then go right and up to the next, all with a 'wraparound' effect). Anyway,…
clovis
  • 325
  • 2
  • 3
  • 13
1
vote
1 answer

2D array diagonal sum

So i am trying to sum the diagonals and compare it to a magic constant number. If they are the same, then it is a magic square. I was able to write the code for the diagonals however it is showing me incorrect result. My code is here: public static…
Saad
  • 399
  • 8
  • 25
1
vote
1 answer

section of my code is being skipped (python 3)

my program is supposed to check whether a matrix is a magic square. however it skips "adding the diagonals" and checking whether all the number (1-16) are in the table. ROWS=4 COLUMNS=4 def main(): tbl=[] numChecktbl=[] for i in range…
jonesie
  • 13
  • 4
1
vote
1 answer

Random magic matrix giving unexpected results

I was trying to create random magic square in Octave and tried something like rand(magic(3)), and it gave unexpected endless results something like this: ans(:,:,1,1,2,1,1,1,1) = 0.894903 0.296415 0.143990 0.186976 0.305691 0.505485 …
1
vote
0 answers

Magic Square Arrays Giving Garbage Data

I'm writing a C++ program that will determine magic squares from a text file. While the size of the matrix prints fine, all my data inside is printed as the same garbage data. Example: Matrix = 3 -858993460 -858993460 -858993460 -858993460…
Ace
  • 31
  • 4
1
vote
0 answers

Magic square with non-consecutive numbers

I am trying to solve a magic square problem with some modification. I have an array of 25 non-consecutive integers, and I have to arrange them in 5x5 2D array so that this array forms a magic square. I have tried brute force algorithm but it is…
SSMA
  • 497
  • 1
  • 7
  • 19
1
vote
2 answers

How could I reverse the output of my Magic Square

I wrote a magic square program and it's run good. But I need to reverse the output of my work into this: (look at the output): Currently my program output is: My desired output is: This is my code at the moment: Scanner input = new…
repsajznav
  • 61
  • 1
  • 8
1 2
3
9 10