Post Reply 
RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
06-01-2019, 04:08 PM
Post: #14
RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
(06-01-2019 03:41 PM)Gerson W. Barbosa Wrote:  Thank you all for your participation and for the interesting and efficient solutions.

Here are my rather convoluted ones:
...

That's an interesting approach, Gerson. I'm a little surprised it did as well as it did, considering the jumps into CAS commands you've got going on. Smile

MOD with approximate numbers is generally faster than MOD with exact integers on the 50g, which is the main reason I used the I→R 10. MOD construct in my original (and subsequent) programs.

Yet another UserRPL take on this that is both shorter and faster:
Code:
\<<
   0. DUPDUP DUP
   5
   6. ROLL 1 SWAP START
      NEXTPRIME
      DUP I\->R 10. MOD
      { 0. 9. 7. 3. 1. } SWAP POS
      DUP 1. +
      ROLL 1. +
      SWAP ROLLD
   NEXT
   DROP
   4. \->LIST
\>>

It definitely dives into the more obscure end of the pool, though. It keeps the counters on the stack instead of contained in composite, and converts the remainder directly into a stack level to determine which counter to increment. This one completes an input of 1000. in about 32s.

Carsen's use of the case statement inspired me to take another look at the performance using that type of construct. I use case structures a lot in SysRPL, but tend not to in UserRPL because they always feel verbose in that environment for some reason. I made another stack-based attempt that uses a case structure for determining which counter to update:
Code:
\<<
   0. DUPDUP DUP
   5
   6. ROLL 1 SWAP START
      NEXTPRIME
      DUP I\->R 10. MOD
      CASE
         1. OVER SAME
         THEN
            DROP
            5. ROLL
            1. +
            5. ROLLD
         END

         3. OVER SAME
         THEN
            DROP
            4. ROLL
            1. +
            4. ROLLD
         END

         7. OVER SAME
         THEN
            DROP
            ROT
            1. +
            UNROT
         END

         DROP
         SWAP
         1. +
         SWAP
      END
   NEXT
   DROP
   4. \->LIST
\>>

While the code size jumped up considerably, the performance actually improved using this method by a few seconds (29s).

I also wanted to see how a SysRPL version would perform:
Code:
::
   CK1NOLASTWD
   CK&DISPATCH1
   real ::
      BINT0 DUPDUP DUP
      Z5_
      6ROLL COERCE ZERO_DO (DO)
         FLASHPTR Prime+
         DUP FLASHPTR Z>R %10 %MOD
         ::
            %1 EQcasedrop :: 5ROLL #1+ 5UNROLL ;
            %3 EQcasedrop :: 4ROLL #1+ 4UNROLL ;
            %7 EQcasedrop :: ROT #1+ UNROT ;
            ( otherwise %9 )
            DROP SWAP #1+ SWAP
         ;
      LOOP
      DROP

      BINT4 ZERO_DO (DO)
         UNCOERCE
         4UNROLL
      LOOP

      BINT4 {}N
   ;
;

It essentially uses the same approach as the previous program, but finishes in about 19s.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - DavidM - 06-01-2019 04:08 PM



User(s) browsing this thread: