Post Reply 
Vietnamese snake puzzle - Closed
05-28-2015, 09:20 PM
Post: #37
RE: Vietnamese snake puzzle - Closed
(05-23-2015 09:08 AM)Werner Wrote:  Including the condition that
Code:
MOD(13*b*i + g*h*c,c*i)=0
after having selected 5 numbers reduced the timing to 30 seconds.

I've changed to the condition FP(13*b/c + g*h/i)=0, which is simpler and uses four multiplication/division operations instead of six. Since I take 13*b/c from the stack, this is reduced to only one multiplication and one division in the inner loop. Also, local variables 'a', 'd' and 'f' are not defined anymore. Running times are now 1h 05m 32.9s on the HP-48GX and 21m 51.9s on the HP 50g. I should revert to my original innermost loop, which apparently is faster, but I prefer to optimize the current one later. I'd be pleased with less than one hour's time on the HP-48GX :-)

Cheers,

Gerson.



HP-48GX:

Code:

%%HP: T(3)A(D)F(.);
\<< TIME { 1 2 3 4 5
6 7 8 9 } { } 1 9
  FOR i 1 9
    FOR c c i \=/
      IF
      THEN 1 9
        FOR b b c \=/
b i \=/ AND
          IF
          THEN b 13
* c / DUP 87 - 1 7
            FOR e e
b \=/ e c \=/ AND e i \=/
AND
              IF
              THEN
e 12 * OVER + 5
PICK 1
\<<
  IF DUP DUP DUP2 i
== SWAP c == OR
SWAP b == OR SWAP e
== OR
  THEN DROP
  END
\>> DOLIST 1 4
FOR j j 1 + 5
  FOR k DUP j GET
OVER k GET DUP2 * i
/ 7 PICK + FP
    IF NOT
    THEN \-> g h
      \<< g h * i / 3
PICK + OVER 1
        \<<
          IF DUP
DUP g == SWAP h ==
OR
          THEN DROP
          END
        \>> DOLIST 0
1 CF
        DO 1 + ROT
ROT SWAP OVER OBJ\->
DROP + NEG + OVER -
ABS .000001 <
          IF
          THEN 1 SF
OVER OBJ\-> DROP SWAP
ROT ROT b c ROT e 5
ROLL g h i 9 \->LIST
1 \->LIST 9 ROLL + 8
ROLLD
          END 1 FS?
NOT
          IF
          THEN SWAP
DUP HEAD SWAP TAIL
SWAP + SWAP
          END ROT
ROT SWAP DUP 3 == 1
FS? OR
        UNTIL
        END 3 DROPN
      \>>
    ELSE DROP2
    END
  NEXT
NEXT DROP2
              END
            NEXT
DROP2
          END
        NEXT
      END
    NEXT
  NEXT SWAP DROP
TIME ROT HMS-
\>>

HP 50g:

Code:

%%HP: T(3)A(D)F(.);
\<< { 1. 2. 3. 4. 5. 6. 7. 8. 9. } { } 1. 9.
  FOR i 1. 9.
    FOR c c i \=/
      IF
      THEN 1. 9.
        FOR b b c \=/ b i \=/ AND
          IF
          THEN b 13. * c / DUP 87. - 1. 7.
            FOR e e b \=/ e c \=/ AND e i \=/ AND
              IF
              THEN e 12. * OVER + 5. PICK 1.
                \<<
                  IF DUPDUP DUPDUP i == SWAP c == OR SWAP b == OR SWAP e == OR
                  THEN DROP
                  END
                \>> DOLIST 1. 4.
                FOR j j 1. + 5.
                  FOR k DUP j GET OVER k GET DUP2 * i / 7. PICK + FP
                    IF NOT
                    THEN \-> g h
                      \<< g h * i / PICK3 + OVER 1.
                        \<<
                          IF DUPDUP g == SWAP h == OR
                          THEN DROP
                          END
                        \>> DOLIST 0. 1. CF
                        DO 1. + UNROT SWAP OVER OBJ\-> DROP + NEG + OVER - ABS .000001 <
                          IF
                          THEN 1. SF OVER OBJ\-> DROP SWAP UNROT b c ROT e 5. ROLL g h i 9. \->LIST 1. \->LIST 9. ROLL + 8. ROLLD
                          END 1. FS? NOT
                          IF
                          THEN SWAP DUP HEAD SWAP TAIL SWAP + SWAP
                          END UNROT SWAP DUP 3. == 1. FS? OR
                        UNTIL
                        END 3. DROPN
                      \>>
                    ELSE DROP2
                    END
                  NEXT
                NEXT DROP2
              END
            NEXT DROP2
          END
        NEXT
      END
    NEXT
  NEXT NIP
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Vietnamese snake puzzle - Dave Britten - 05-20-2015, 11:27 AM
RE: Vietnamese snake puzzle - Gerald H - 05-20-2015, 11:33 AM
RE: Vietnamese snake puzzle - Gerald H - 05-20-2015, 12:58 PM
RE: Vietnamese snake puzzle - Closed - Gerson W. Barbosa - 05-28-2015 09:20 PM



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