Post Reply 
HHC 2019 will be in Reno, Nevada, Sept 21-22
09-27-2019, 11:07 PM
Post: #50
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22
(09-23-2019 01:54 PM)Claudio L. Wrote:  My take, nothing clever here, just plain brute force.

OK, now it's time to get a bit clever.
Below is a version of Wes's brute force, but this time it precomputes all multiplications and powers and stores them in lists.
This brings down execution time down to 49 ms (from 68 ms), 27% faster at the expense of some memory.

Code:

«
  { 0 1 2 3 4 5 6 7 8 9 } 'U' LSTO     @ Save the units
   U 10 * 'T' LSTO                           @ Save the tens
   T 10 * 'H' LSTO                           @ Save the hundreds
   U 3 ^ 'C' LSTO                            @ Save the cubes
   2 10 FOR 'X'
     1 10 FOR 'Y'
         1 10 FOR 'Z'
             U Z GET
             T Y GET
             H X GET
             + +                    @ Compute the number from its digits
             C Z GET
             C Y GET
             C X GET
            + +                     @ Compute the sum of cubes
            IF OVER ≠ THEN DROP END
      NEXT
    NEXT
  NEXT
»

And talking about clever... here comes David. The code below is identical except it does David's early exit trick, bringing down total execution time now to 35 ms.

Code:

«
  { 0 1 2 3 4 5 6 7 8 9 } 'U' LSTO     @ Save the units
   U 10 * 'T' LSTO                           @ Save the tens
   T 10 * 'H' LSTO                           @ Save the hundreds
   U 3 ^ 'C' LSTO                            @ Save the cubes
   2 10 FOR 'X'
     1 10 FOR 'Y'
         1 10 FOR 'Z'
             U Z GET
             T Y GET
             H X GET
             + +                    @ Compute the number from its digits
             C Z GET
             C Y GET
             C X GET
            + +                                    @ Compute the sum of cubes
            IF OVER - DUP THEN                     @ Keep the difference of the Sum(cubes)-Number
                  NIP IF 9 > THEN EXIT END         @ If the difference is more than 9, do early exit
            ELSE
                 DROP 
            END
      NEXT
    NEXT
  NEXT
»
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22 - Claudio L. - 09-27-2019 11:07 PM



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