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

Maple magic square

Ok I really need your help guys I'm really lost this looks so simple but I can't figure it out. Note : this is for 3x3 magic square So here's the condition for a magic square : 1. the sum of the elements of a row = k 2.the sum of the elements of a…
0
votes
1 answer

ALL solutions to Magic square using no array

Yes, this is for a homework assignment. However, I do not expect an answer. I am supposed to write a program to output ALL possible solutions for a magic square displayed as such: +-+-+-+ |2|7|6| +-+-+-+ |9|5|1| +-+-+-+ |4|3|8| …
Smoak
  • 25
  • 6
0
votes
1 answer

Magic Square Program Outputs Incorrect Sum Values for Rows, Columns, Diagonals

I coded a Magic Square program with a driver and a class. The only problem I'm having is adding up the rows, columns, and diagonals. I made a int constant called "col" for column, that counts up 1 each time an entire row is summed. Therefore, the…
Alan Navai
  • 49
  • 11
0
votes
2 answers

Magic Square final value always true

For class we have to do a magic square. I got my code somewhat working but it does multiple squares and the final one always returns 0, Here is how a magic square works. Here is how I check public int downDiagSum() { int sum = 0; for(int r = 0; r…
0
votes
1 answer

Can't convert a string into a list of integers

I am trying to make a program in python that identifies whether a square is a magic square or not and i am having trouble getting the user input into a list. I understand that my code could be more efficient but I am very new to python. column_1 =…
0
votes
0 answers

Construct 4 x 4 magic square with fixed location for “1”

The method I have found to generate 4 by 4 magic squares gives me a result in which the number "1" is at of the corners of the square. How can we extend this to a method to generate a magic square, for a fixed location of number "1"? The number "1"…
0
votes
1 answer

Magic Square Code Help, want to know where it moves the number down Programming in C

# include # include int main(void) { int s; int row; int column; int k; int array[99][99] ; printf("Enter the dimension of the square : ") ; scanf("%d", &s) ; if (s % 2 == 0) { printf("Please enter an even number") ; goto…
rprog
  • 1
  • 3
0
votes
0 answers

Magic square calculation 2 dimensional array C++

So I need to make a code with these functions. the checkMatrix has to be a boolean and the getData has to pass the 1D array. How do I check the magic square with a 2D array? instructions :Write a program that will prompt the user for the integers…
0
votes
1 answer

Generating random magic squares

I am wondering how to create a matrix with random natural numbers but with the sum of every column equal to sum of every row and diagonal sum also. I mean that you create a function that by choosing dimension and sum of the rows, columns and…
Artur Si
  • 31
  • 1
0
votes
1 answer

Create function for magic square in Matlab

I have created a function for a magic square in matlab of size n, initialized with the zeros() command, and with a for loop using an iterator i. The output I am getting is correct but I cannot figure out how to fix the issue of if the diagonal aka…
user4888
  • 101
  • 1
  • 12
0
votes
1 answer

Magic Square NxN

I'm new to Prolog and I'm trying to write fully working magic square program, but to say the truth I don't really know how to do, I have started but I feel that I'm doing it wrong. I'm sharing my code and I hope someone will help me, now when…
EasyCode
  • 47
  • 1
  • 8
0
votes
2 answers

Is it possible to condense my code into less than 3 lines?

public class MagicSquare { public static int[][] grid = new int[3][3]; public static int i = 0; public static int j = 0; public static void main(String[] args) { int x = 1; int y = 2; int z = 0; while(z…
0
votes
1 answer

Writing a function that returns boolean true if matrix forms a magic square without importing numpy

I already wrote one part of the program which is below: def matrix_is_square(matrix): for i in range(len(matrix)): if len(matrix[i]) != len(matrix): return False return True This function returns True if matrix is a square…
user6945769
0
votes
1 answer

Formatting This Magical Square

I am looking to format the output of this code so that the totals of the rows and columns show up next to/below said row/column. I am getting the numbers correct, but spaces and char counts seem to be too unpredictable to format it correctly for any…
Parker Harris
  • 155
  • 1
  • 7
0
votes
1 answer

write a code in a more simpler way

int[][] board = new int[i][i]; int row = 0; int col = i / 2; int num = 1; while (num <= i * i) { board[row][col] = num; num++; int tCol = (col + 1) % i; int tRow = (row - 1) >= 0 ? row - 1 : i - 1; if (board[tRow][tCol] != 0) { …