Sunday, October 22, 2017

HP Prime and Casio fx-CG 50: 3 x 3 Magic Squares

HP Prime and Casio fx-CG 50: 3 x 3 Magic Squares

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

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;

Casio fx-CG50G Program:  MAGICSQ3

"2017-10-06 EWS"
RanInt#(-5,200)->R
RanInt#(1,3)->K
Identity 3->Mat A
Seq(K*X,X,R-4*K,R+4*K,K)->List 1
Sum List 1->S
S/3->T
List 1[5]->Mat A[2,2]
List 1[1]->Mat A[1,1]
List 1[9]->Mat A[3,3]
List 1[2]->Mat A[1,3]
List 1[8]->Mat A[3,1]
T-Mat A[1,1]-Mat A[1,3]->Mat A[1,2]
T-Mat A[1,1]-Mat A[3,1]->Mat A[2,1]
T-Mat A[1,3]-Mat A[3,3]->Mat A[2,3]
T-Mat A[3,1]-Mat A[3,3]->Mat A[3,2]
If RanInt#(0,1)=1
Then
Trn Mat A->Mat A
IfEnd
"TARGET SUM:"
T
Mat A

Note:  = Disps

Your results will vary.  Happy Halloween (one week to go!)

Sources:

Learn-With-Games.Com  “Magic Square Solution”  http://www.learn-with-math-games.com/magic-square-solution.html  Retrieved October 6, 2017
William H. Richardson.  “Non-Normal Magic Squares.”  http://www.math.wichita.edu/~richardson/mathematics/magic%20squares/notnormalmagicsquare.html  Retrieved October 22, 2017

Eddie


This blog is property of Edward Shore, 2017.

HHC 2025 Videos

  HHC 2025 Videos The talks from the HHC 2025 conference in Orlando, Florida are starting to be up on hpcalc’s YouTube page within th...