HP Forums
(49G) Golomb Sequence - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (49G) Golomb Sequence (/thread-4589.html)



(49G) Golomb Sequence - Gerald H - 08-28-2015 04:05 PM

Golomb's sequence: 1, 2, 2, 3, 3, 4, 4 ... consists of elements indicating the number of occurences of its index in the list, index commencing at 1.

The programme gives exact correct integers for indices to 10^12, thereafter approximate.

Enter integer index to return the number of its appearances in the sequence.

Code:

::
  CK1&Dispatch
  # FF
  ::
    '
    ::
      DUP
      % .618322
      %^
      % 1.2
      %*
      % .513
      %+
      %IP
      {
        % 423.
        % 507.
        % 597.
        % 709.
        % 828.
        % 954.
      }
      ROT
      EQUALPOSCOMP
      #0=?SEMI
      %1+
    ;
    SWAP
    FPTR2 ^Z>R
    %ABS
    DUP
    % 999999999999.
    %>
    case
    ::
      SWAP
      EVAL
    ;
    DUP
    % 1087.
    %<
    ITE
    ::
      SWAP
      EVAL
    ;
    ::
      %1
      DUP
      DUPDUP
      4NULLLAM{}
      BIND
      %1-
      BEGIN
      1GETLAM
      4GETLAM
      %1+
      DUP4PUTLAM
      4PICK
      EVAL
      DUP
      3PUTLAM
      %.5
      DUPUNROT
      %*
      %+
      %+
      3GETLAM
      DUP
      1GETLAM
      %+
      1PUTLAM
      4GETLAM
      %*
      DUP
      2GETLAM
      %+
      2PUTLAM
      %*
      OVERUNROT
      %-
      DUP
      %0>
      WHILE
      SWAPDROP
      REPEAT
      ROTDROP
      3GETLAM
      DUP
      %CHS
      1GETLAM
      %+
      1PUTLAM
      4GETLAM
      %*
      DUP
      %CHS
      2GETLAM
      %+
      2PUTLAM
      SWAPDROPSWAP
      BEGIN
      DUP
      1GETLAM
      %1+
      DUP1PUTLAM_
      4GETLAM
      DUP
      2GETLAM
      %+
      2PUTLAM
      %*
      %-
      DUP
      %0>
      WHILE
      SWAPDROP
      REPEAT
      DROPSWAPDROP
      1GETLAM
      DUPUNROT
      %+
      %1-
      SWAP
      %/
      %IP
      2GETLAM
      %+
      4GETLAM
      ABND
      %-
    ;
    FPTR2 ^R>Z
  ;
;