Post Reply 
RPL second impressions (HP 28)
07-01-2018, 12:41 PM (This post was last modified: 07-01-2018 12:50 PM by Gerson W. Barbosa.)
Post: #36
RE: RPL second impressions (HP 28)
(07-01-2018 03:49 AM)Thomas Klemm Wrote:  
(06-28-2018 08:48 PM)mdunn Wrote:  All previous ones don’t work either when n<4.

There's a problem with the FOR-loop in that it gets executed at least once even when the upper limit is smaller than the start value.

...

Now you can either handle that special case beforehand or then just call it a day.
How often will you use that program to check that 3 is a prime?

When I said "all previous ones" that included my original program. As I said, that limitation was irrelevant to my purpose, but if I want to use it as a primality test then I think it should handle all valid arguments, even if we know 1 is not prime and 2 and 3 are. An easy way to do it is by means of a patch (thus, an inelegant solution) to be added at the beginning of the program:

« 
  IF 3 DUP2 ≤
  THEN SWAP - 
  ELSE DROP
  END

  IF DUP 2 MOD
  THEN → n
    «
      IFERR 3 n √
        FOR d 
          n d MOD INV DROP 2
        STEP
      THEN
      ELSE 1
      END
    »
  ELSE NOT
  END
»


I don't use the 28S anymore for prime factorization, as the built-in FACTOR on the 50g does it much faster, but if the 28S were my only calculator I would use your optimized version without my unnecessary patch.

FWIW, here is my full original HP-28S program:

Code:

%%HP: T(3)A(R)F(.);
DIR
  PRFAC
  \<< DUP
    IF NOT PRIME
    THEN 1 SF 2 "'" ROT ROT DCMP 1 + 1 CF
      WHILE OVER 1 \=/
      REPEAT DCMP 2 +
      END DROP2 DUP SIZE 1 - 1 SWAP SUB "'" + STR\->
    END
  \>>
  DCMP
  \<< DUP2 MOD
    IF NOT
    THEN DUP \->STR 0
      DO 1 + 4 ROLL 4 ROLL SWAP OVER / DUP2 6 ROLLD 5 ROLLD SWAP MOD
      UNTIL NOT NOT
      END DUP
      IF 1 \=/
      THEN SWAP "^" + SWAP \->STR +
      ELSE DROP
      END ROT DUP
      IF PRIME
      THEN DUP 2 - 1 FS? + ROT 4 ROLL DROP
      ELSE ROT ROT
      END "*" + 4 ROLL SWAP + ROT ROT
    END
  \>>
  PRIME
  \<< DUP 2 / FP
    IF NOT NOT
    THEN DUP \v/ 1
      DO 2 + 3 DUPN SWAP OVER
      UNTIL < ROT ROT MOD NOT OR
      END SWAP DROP MOD NOT
    END NOT
  \>>
END

Thank you very much for your advices and improvements.

Gerson.

PS:

Quote:All previous ones don’t work either when n<4.

That was written by myself, not by mdunn.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RPL second impressions (HP 28) - mdunn - 06-27-2018, 01:19 AM
RE: RPL second impressions (HP 28) - mdunn - 06-27-2018, 01:58 PM
RE: RPL second impressions (HP 28) - mdunn - 06-27-2018, 04:06 PM
RE: RPL second impressions (HP 28) - mdunn - 06-27-2018, 05:11 PM
RE: RPL second impressions (HP 28) - mdunn - 06-27-2018, 07:45 PM
RE: RPL second impressions (HP 28) - mdunn - 06-28-2018, 08:48 PM
RE: RPL second impressions (HP 28) - Gerson W. Barbosa - 07-01-2018 12:41 PM
RE: RPL second impressions (HP 28) - ttw - 07-04-2018, 10:52 PM



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