All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Numbers
Disk Magic (Posted on 2012-11-30) Difficulty: 4 of 5

No Solution Yet Submitted by brianjn    
No Rating

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution computer solutions | Comment 1 of 2

Both 48 and 45 seem possible as a magic sum. They are obtainable in numerous ways each.  Here are a sampling:

The array of black numbers is followed by the magic square and its magic constant:

 1  2  2  3  4  3  4  2 
 4  2  5  7  4  5  5  3
 6  4  2  5  2  5  2  1
 2  5  5  3  5  3  2  3
 4  3  7  2  6  5  2  5
 2  5  3  1  4  4  4  3
 6  3  4  6  3  6  5  7
 2  6  3  2  4  6  4  6
 
 13  18  17
 20  16  12
 15  14  19
 
 48
 1  2  3  7  3  5  2  3 
 4  2  2  5  4  4  4  5
 4  5  3  5  3  5  3  2
 6  2  5  2  5  2  1  2
 3  5  7  2  6  5  3  4
 4  2  3  1  4  4  5  2
 2  6  3  4  4  3  6  4
 6  3  2  6  6  6  7  5
 
 12  17  16
 19  15  11
 14  13  18
 
 45
 


The program checked that the integers in the magic square were distinct, but not that they were consecutive, but only a handful of extraneous (non-consecutive numbers) solutions came up. The 45's and the 48's both had consecutive numbers.

DECLARE SUB rotate (cr!, cc!)
CLEAR , , 25000

DIM SHARED cgrid(4, 4) AS STRING, ngrid(8, 8), sgrid(4, 4)

DATA 1224,5237,3544,2354
DATA 4526,2535,2535,2132
DATA 5243,7213,6544,4253
DATA 2636,2346,6643,4576

FOR crow = 1 TO 4
    FOR ccol = 1 TO 4
        READ cgrid(crow, ccol)
    NEXT
NEXT

OPEN "diskmagc.txt" FOR OUTPUT AS #2
CLS

rotate 1, 1

END

SUB rotate (cr, cc)
FOR ramt = 1 TO 4
    IF cr = 1 AND cc <= 2 THEN PRINT cr; cc, ramt
    c$ = MID$(cgrid(cr, cc), ramt) + LEFT$(cgrid(cr, cc), ramt - 1)
    ngrid(2 * cr - 1, 2 * cc - 1) = VAL(MID$(c$, 1, 1))
    ngrid(2 * cr - 1, 2 * cc) = VAL(MID$(c$, 2, 1))
    ngrid(2 * cr, 2 * cc - 1) = VAL(MID$(c$, 4, 1))
    ngrid(2 * cr, 2 * cc) = VAL(MID$(c$, 3, 1))

    sgrid(cr - 1, cc - 1) = sgrid(cr - 1, cc - 1) + ngrid(2 * cr - 1, 2 * cc - 1)
    sgrid(cr - 1, cc) = sgrid(cr - 1, cc) + ngrid(2 * cr - 1, 2 * cc)
    sgrid(cr, cc - 1) = sgrid(cr, cc - 1) + ngrid(2 * cr, 2 * cc - 1)
    sgrid(cr, cc) = sgrid(cr, cc) + ngrid(2 * cr, 2 * cc)

    good = 1
    IF (cr = 3 OR cr = 4) AND cc = 4 THEN
        s1 = sgrid(cr - 1, 1) + sgrid(cr - 1, 2) + sgrid(cr - 1, 3)
        s2 = sgrid(cr - 2, 1) + sgrid(cr - 2, 2) + sgrid(cr - 2, 3)
        IF s1 <> s2 THEN
            good = 0
        END IF
        ssave = s1
    END IF
    IF good THEN
        IF cr = 4 AND cc = 4 THEN
            s1 = sgrid(1, 1) + sgrid(2, 2) + sgrid(3, 3)
            s2 = sgrid(3, 1) + sgrid(2, 2) + sgrid(1, 3)
            IF s1 <> s2 OR ssave <> s1 THEN
                good = 0
            END IF
            IF good THEN
                REDIM had(9)
                FOR rw = 1 TO 3
                    FOR cl = 1 TO 3
                        had(3 * rw - 3 + cl) = sgrid(rw, cl)
                    NEXT
                NEXT
                FOR i = 1 TO 8
                    FOR j = i + 1 TO 9
                        IF had(i) = had(j) THEN good = 0
                    NEXT j
                NEXT i
                s1 = sgrid(1, 1) + sgrid(2, 1) + sgrid(3, 1)
                s2 = sgrid(1, 2) + sgrid(2, 2) + sgrid(3, 2)
                s3 = sgrid(1, 3) + sgrid(2, 3) + sgrid(3, 3)
                IF s1 <> s2 OR s2 <> s3 THEN good = 0
                IF good THEN
                    PRINT #2,
                    PRINT
                    FOR rw = 1 TO 8
                        FOR cl = 1 TO 8
                            PRINT #2, ngrid(rw, cl);
                            PRINT ngrid(rw, cl);
                        NEXT
                        PRINT #2,
                        PRINT
                    NEXT
                    FOR rw = 1 TO 3
                        FOR cl = 1 TO 3
                            PRINT sgrid(rw, cl);
                            PRINT #2, sgrid(rw, cl);
                        NEXT
                        PRINT
                        PRINT #2,
                    NEXT
                    PRINT s1
                    PRINT #2, s1
                END IF
            END IF
        ELSE
            ccnew = cc + 1
            IF ccnew > 4 THEN
                ccnew = 1: crnew = cr + 1
            ELSE
                crnew = cr
            END IF
            rotate crnew, ccnew
        END IF

    END IF


    sgrid(cr - 1, cc - 1) = sgrid(cr - 1, cc - 1) - ngrid(2 * cr - 1, 2 * cc - 1)
    sgrid(cr - 1, cc) = sgrid(cr - 1, cc) - ngrid(2 * cr - 1, 2 * cc)
    sgrid(cr, cc - 1) = sgrid(cr, cc - 1) - ngrid(2 * cr, 2 * cc - 1)
    sgrid(cr, cc) = sgrid(cr, cc) - ngrid(2 * cr, 2 * cc)


NEXT

END SUB

 


  Posted by Charlie on 2012-11-30 17:36:39
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (7)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2017 by Animus Pactum Consulting. All rights reserved. Privacy Information