Re: a new challenge! Message #37 Posted by Alex G on 25 July 2010, 5:28 p.m., in response to message #24 by David Hayden
Hi - I am a newbie to RPL and the HP50g, though I have a smattering of functional programming understanding, and I really like what I've seen so far. Thanks to David and others, who've been useful signposts on the early stages of the road.
Here's my approach. I split it up into 3 programs, I'm a great believer in readability and re-use.
First the main proggy, FINDSPPR, to find that 'special' prime:
« DO NEXTPRIME
UNTIL DUP SPECPR
END
»
The above calls the function SPECPR, which returns 1 if the prime is 'special', 0 otherwise.
« DUP INT2LIST
« DUP ! +
» MAP sigmaLIST ==
»
(Lovely to have MAP available, BTW, FILTER is easy to write, too)
Finally INT2LIST, which turns the integer to a string of digits.
Had a bit of trouble here, but in the end I used David's approach.
« DUP SIZE -> N
« 1. N
FOR J DUP 10. MOD SWAP 10. / IP
NEXT DROP N ->LIST REVLIST
»
»
27 minutes for the above! Too much really, for an ARM machine, but then , I suppose, I should remember it's an ARM emulating a Saturn.
It would be nice to have the INT2LIST function built-in, or at least, maybe at SysRPL level. I found out a lot about the calc while writing it, and in a way, it was the biggest time-hog for this program.
My initial approach was recursive, using lists, and took about four times as long as "David's", above. I realised iteration would be quicker. I also used STO+ with lists, but this was still too slow. Then I realised that my indefinite WHILE loop was still taking too long. It was worth getting the SIZE and using a FOR (75% speedup). Finally, my intial approach was to use IDIV2 rather than MOD and IP, falsely thinking it would be faster. Wrong.
« DUP SIZE -> N
« 1. N
FOR J 10. IDIV2 SWAP
NEXT DROP N ->LIST REVLIST
»
»
Takes almost twice as long as the version finally used.
Now, I do realise that there are a whole lot of clever optimizations and/or assumptions available to sped things up, but I was really looking for a simple answer, without such ingenuities.
Much as I am liking RPL I think the speed issue is a huge problem, I wrote a similar program to the one above on my PDA using Scheme and SLIB and it ran in seconds! Hmm, I see there is an RPL/2 available out there... (lamp emoticon here)
Edited: 25 July 2010, 5:49 p.m. after one or more responses were posted
|