CY's Brute-Force Take on Task 2 of PWC#089

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenge, submit codes on-time (via GitHub or email).



(In a rush, sorry for the "raw" style this week.)


a b c
d e f
g h i

Statement: The center term, "$e" , is 5.
Proof:
Given the sum of each "segment" is 15.

Consider
sum of 2 outer rows - the middle column - sum of 2 diagonals
= (2-1-2)*15

Then we have
(a+b+c + g+h+i) - (b+e+h) - (a+e+i + c+e+g) = (2-1-2)*15 -3*e = -15 e = 5 □ Then loop the values of $a and $b over 1 to 9 $i = 10-$a $c = 15-$a-$b $h = 10-$b $g = 10-$c $d = 15-$a-$g $f = 15-$c-$i
Then check whether the values of variables match the 8 given equations and use every integer from 1 to 9 .

I got 8 magic squares.

----------------------

The definition of "magic cube" as recorded on Wikipedia, early December 2020, is : "the sums of the numbers on each row [9 rows], on each column [9 columns], on each pillar [9 pillars] and on each of the four main space diagonals [4 space diagonals] are equal to the same number." And I think we may explore 3x3x3 cube in a similar fashion.

first (top) layer
a b c 
d e f 
g h i
 
second (middle) layer
j k l 
m X n 
o p q 

third (bottom) layer
r s t 
u v w 
x y z 


1+2+3+..+27 = 27*28/2 = 27*14
There are exactly 9 pillars composing the cube. 
Since the sums of each pillar are the same, we have 

             27*14/9 = 42 ,

as the sum of each "segment".


Statement: The center term, "$X", is 14.
Proof:

 Consider:

 (sum of 4 corner pillars)
- (sum of 2 outer columns of the middle layer) + (m+X+n)
 - (sum of 2 outer columns of the middle layer)
 - (sum of 4 space diagonals)
(4+2+1-2-4)*42 = a+j+r + c+l+t + i+q+z + x+o+g
 - (j+m+o) -(l+n+q) + m+X+n
- (a+X+z) - (c+X+x) - (i+X+r) - (g+X+t)
= -3X
(A bit worry about making errors during cancelling terms?
Use a computer algebra system.) -42 = -3X X = 14 □
Then loop the values of $e,$a,$b,$g from 1 to 27 $c = 42 - $a - $b $z = 28 - $a $x = 28 - $c $y = 42 - $x - $z $d = 42 - $a - $g $t = 28 - $g $w = 42 - $z - $t $h = 42 - $e - $b $p = 42 - $y - $h $f = 42 - $d - $e $i = 42 - $c - $f $r = 28 - $i $s = 42 - $t - $r $v = 42 - $y - $s $j = 42 - $a - $r $l = 42 - $c - $t $k = 42 - $j - $l $q = 42 - $z - $i $n = 42 - $l - $q $m = 28 - $n $o = 42 - $j - $m $u = 42 - $v - $w


I got 192 different magic cubes.
One of them is
 8 10 24 
 22 9 11
 12 23 7

 13 27 2 
 3 14 25
 26 1 15

 21 5 16 
 17 19 6
 4 18 20

---
link for codes: ch-1.pl (Task 1 on gcd sum) , ch-2.pl (Task 2; the 3x3x3 magic cube code is on POD of the bottom of the file)

Do tell or correct me, if you have oppositions, want to discuss or give me advice!

Stay healthy! □

(Note after 6-hour published: should write more caution or analysis on this task (Task 2 #089) and Sudoku (Task 2 #086) in next blogpost! )

Leave a comment

About C.-Y. Fung

user-pic This blog is inactive and replaced by https://e7-87-83.github.io/coding/blog.html ; but I post highly Perl-related posts here.