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
votes
1 answer

Magic square array filled with random numbers doesn't accept even numbers

I found this code that i need for an assigment, but it only reads odd numbers and i need it to read even numbers too, but i don't know whats wrong. I need it to make the random magic squares go from 1 to 10. Still very much a beginner and don't…
daniell e
  • 17
  • 2
-1
votes
2 answers

How do I create a magic square matrix using python

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the eggs in the basket such that the sum of each row,…
-1
votes
1 answer

How do I print out the value that make magic square?

I have tried this code that I found online and it worked, but I want it to print out which number makes magic square. In this case it is 83, so instead of cout<<"Magic Square", how do I change it to show 83 instead? Thank you in advance. # define…
Nirl
  • 3
  • 3
-1
votes
1 answer

Making different sizes of magic square

I'm making different sizes of magic square and want to get the values that the sum of each columns, rows and diagonal lines are the same in Javascript. Here is my code: var numsarray = []; window.boxes = document.querySelectorAll(".box") let time…
Shawn
  • 132
  • 1
  • 1
  • 13
-1
votes
3 answers

Why still giving type error?

I am making a python program to check if the given values represent magic square import math r1=[8,1,6] r2=[3,5,7] r3=[4,9,2] c1=zip(r1[:1],r2[:1],r3[:1]) c1=…
Bhoomika Sheth
  • 323
  • 5
  • 17
-1
votes
4 answers

Fill Hash with nil values if no values given

I have these arrays: positions = [[0, 1, 2], [2, 3]] values = [[15, 15, 15], [7, 7]] keys = [1, 4] I need to create a hash whose keys are from keys and the values are from values. Values must be at indices defined in positions. If no index is…
Julius Dzidzevičius
  • 10,775
  • 11
  • 36
  • 81
-1
votes
1 answer

Spiral Magic Square in C

Ok so i wanna make a code thats gonna print a magic square that works like the one in the picture (https://i.stack.imgur.com/q3vud.jpg) ( which is a 5x5 matrix ) but it is 11x11.Code seems fine to me but unfortunately it doesnt work , can anyone…
-1
votes
2 answers

Magic Squares Print Square

This is my print square method public void printSquare() { DecimalFormat newSquare = new DecimalFormat("00"); for (int row = 0; row < square.length; row++) { for (int col = 0; col < square[row].length; col++) { …
-1
votes
1 answer

Magic Square Code (java)

I've been having an issue with my Magic Square code. It continually prints "This is a magic square" even when I'm positive it isn't. You enter 16 integers, and then the code is supposed to run and determine whether the entered integers create a…
adlerT16
  • 1
  • 1
  • 1
  • 3
-1
votes
1 answer

There's something wrong in my Magic Square Algorithm

Hi I created a Magic Square program in java It works fine if you input a number 3 but if i input 5 and so on there's a problem occurring.. The pattern becomes wrong. Please help me to find out what's wrong in my code: Here's my code: Scanner…
repsajznav
  • 61
  • 1
  • 8
-1
votes
1 answer

Program To Test whether or not a 4x4 matrix is a magic square using functions in C++

Here is what I have. I have it outputting most of the sums just so i can check there values. I think the problem is with the value of the elements in the array storing the column sums. I would greatly appreciate any feedback. #include…
-1
votes
1 answer

Magic Square using Particle Swarm Optimization

I am given a n x n matrix where n ranges from 3 to 5. The matrix is then assigned random values from 1-n^2. Given the matrix I am to optimize the board such that I arrive to a solution of magic square. Random board example: n = 3 3 5 6 1 7…
Holmes
  • 53
  • 6
-1
votes
2 answers

Verifying user input for a magic square game/Comparing arrays (user-defined&programmer-defined)

I am trying to program a very simple 3x3 magic square game using arrays. My program will ask the user thrice to input three numbers each time (for the top row, middle row, bottom row) and then the values will be displayed. The problem is I'm trying…
-1
votes
1 answer

Is this a logic error with this code for a Magic Square Java program?

I have written a program that creates a nxn matrix, then checks if the matrix is a magic square. In my method magicCheck, all elements are totaled (sum) then divided by n (the number of rows or columns) to generate a magicNumber. This magicNumber is…
woollyMammoth
  • 105
  • 1
  • 2
  • 7
-2
votes
1 answer

Magic Square in Java: Why always prints: Magic Squares cannot be created?

In the code below i try to read a file with the size of a 2d array and the numbers for each cell. Then the program must find a number that can be added in the cell with a zero value in magic array. All this in order to create magic squares based on…
athanasia
  • 1
  • 1
1 2 3
9
10