The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Magic Squares
10-22-2017, 03:34 PM
Post: #1
Magic Squares
Link to blog entry: http://edspi31415.blogspot.com/2017/10/h...magic.html

Background

A magic square is a square of integers where each row, column, and diagonal have the same sum. For example:

2 9 4
7 5 3
6 1 8

Each row, column, and diagonal of this magic square has a sum of 15.

A proper magic square has the integers 1 through n^2, n is the size of the magic square. A non-normal magic square follows the sum rule, but different integers than the 1 to n^2 sequence is allowed.

The program MAGICSQ3 generates random 3 x 3 non-normal magic squares. The sum of each row, column, and diagonal are given.

HP Prime Program: MAGICSQ3
Code:

EXPORT MAGICSQ3()
BEGIN

// Random Magic Square 3 X 3
// 2017-10-06 EWS
LOCAL x,k,r,mat,s,t,l;

// Initialization
r:=RANDINT(−5,200);
k:=RANDINT(1,3);
l:=MAKELIST(k*X,X,r-4*k,
r+4*k,k);
l:=SORT(l);
s:=ΣLIST(l);
t:=s/3;
mat:=MAKEMAT(0,3,3);

// Generation
mat(2,2):=l(5);
mat(1,1):=l(1); 
mat(3,3):=l(9);
mat(1,3):=l(2);
mat(3,1):=l(8);
mat(1,2):=t-mat(1,1)-mat(1,3);
mat(2,1):=t-mat(1,1)-mat(3,1);
mat(2,3):=t-mat(1,3)-mat(3,3);
mat(3,2):=t-mat(3,1)-mat(3,3);

// Tranpose?
IF RANDINT(0,1) THEN
mat:=TRN(mat);
END;

// Results
RETURN {mat,t};

END;
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Magic Squares - Eddie W. Shore - 10-22-2017 03:34 PM



User(s) browsing this thread: 1 Guest(s)