RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
06-01-2019, 04:08 PM
Post: #14
 DavidM Senior Member Posts: 989 Joined: Dec 2013
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.

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.
 « Next Oldest | Next Newest »

 Messages In This Thread RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 03:07 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Valentin Albillo - 06-01-2019, 04:13 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 04:43 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - DavidM - 06-01-2019, 05:02 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gilles - 06-01-2019, 05:29 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Albert Chan - 06-01-2019, 12:42 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 05:44 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Carsen - 06-01-2019, 08:51 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - grsbanks - 06-01-2019, 12:48 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Valentin Albillo - 06-01-2019, 02:38 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 03:29 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Valentin Albillo - 06-01-2019, 09:59 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-02-2019, 10:43 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Valentin Albillo - 06-02-2019, 11:27 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-03-2019, 12:14 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Valentin Albillo - 06-03-2019, 04:25 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 03:17 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 03:41 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - DavidM - 06-01-2019 04:08 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-01-2019, 09:11 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Dave Britten - 06-01-2019, 11:05 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-02-2019, 06:10 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - John Keith - 06-02-2019, 04:39 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gilles - 06-02-2019, 09:18 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Carsen - 06-03-2019, 03:49 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-03-2019, 01:08 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Carsen - 06-03-2019, 08:15 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Gerson W. Barbosa - 06-04-2019, 03:00 AM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - fred_76 - 06-04-2019, 02:21 PM RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - ttw - 06-04-2019, 05:09 PM

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