Post Reply 
RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
06-01-2019, 03:17 PM
Post: #11
RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
(06-01-2019 08:51 AM)Carsen Wrote:  Hi Everyone.

Here's my take on the challenge. The algorithm is very straightforward. Get the next prime number, put it in a string, retrieve the last digit in the string, test if the digit is a 1, 3, 7, 9, or none of the above, & then add it to the list.

Code:

<<   { 0 0 0 0 }  5 ROT 1 SWAP
START NEXTPRIME DUP ->STR DUP
SIZE DUP SUB STR->
CASE DUP 1 ==
THEN { 1 0 0 0 }
END DUP 3 ==
THEN { 0 1 0 0 }
END DUP 7 ==
THEN { 0 0 1 0 }
END DUP 9 ==
THEN { 0 0 0 1 }
END DUP ==
THEN { 0 0 0 0 }
END
END NIP ROT ADD SWAP NEXT DROP
>>

Not great performance in Exact mode, hitting 151.9 seconds with 1000 primes starting with 7. However, in Approx. mode, I get a decent time of 86.5 seconds.

Thanks for the challenge Gerson. I had fun.

I am glad you had fun. That was the idea, hence an 'exercise', not a 'challenge'.

Just two remarks:

1) The STR-> before should be an OBJ-> (just a manual listing mistake);

2) You can omit the optional case clause, DUP == THEN { 0 0 0 0 } END (see grsbanks's comment below in this thread). Thus your byte count will be under 200 and you can save about three seconds in Exact Mode.

I had considered testing this CASE structure, but I didn't. Thanks for doing it. I would get about the same running times, but at least I could have tried Approximate Mode, which is not possible in my programs.

Gerson.
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) - Gerson W. Barbosa - 06-01-2019 03:17 PM



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