Post Reply 
OEIS featured in The New York Times
07-25-2024, 08:32 PM
Post: #13
RE: OEIS featured in The New York Times
(07-24-2024 09:08 AM)Gil Wrote:  But in fact, no necessity to save the last element calculated, we can just play on its duplication. The above program becomes then:
Code:
\<< DUP 1 \-> n p
  \<< 1 SWAP 1 \=/
    IF
    THEN 1 n 1 -
      START DUP DUP 2 MOD NOT
        IF
        THEN 2 /
        ELSE p NEXTPRIME DUP 'p' STO +
        END
      NEXT
    END n \->LIST
  \>>
\>>

Nice program, simple and fast. Here is a slightly modified version to illustrate a couple of memory saving tips. This is not meant as criticism of your program, just as information that may be of help to you and others.

Code:

\<< DUP 1 \-> n p
  \<< 1 SWAP 1
    IF \=/
    THEN 2 n
      START DUPDUP 2 MOD
        IF NOT
        THEN 2 /
        ELSE p NEXTPRIME DUP 'p' STO +
        END
      NEXT
    END n \->LIST
  \>>
\>>

My first tip turns out not to save any memory in this case but deserves mention. I have always heard that in IF..THEN..(ELSE)..END structures, having exactly one object between IF and ELSE minimizes memory use. It seems that your method of having nothing between IF and ELSE uses no more memory. I do think that my version is more readable but that may not be the case with other programs.

My next tip is to use 2 n START rather than 1 n 1 - START which does the same number of iterations but saves 5 bytes. Note that this may not work with FOR loops where the value of the looping variable would be different, but is always safe to use with START loops.

A final, simple tip is to use the extended stack operations available on the HP 49 and 50. In this case I replaced DUP DUP with DUPDUP, saving 2.5 bytes. Others include UNROT, PICK3 and NIP. These extended stack operations are faster than their equivalents, as well as saving memory.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: OEIS featured in The New York Times - John Keith - 07-25-2024 08:32 PM



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