(49G) Moser de Bruijn Sequence, OEIS A000695

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html)
+--- Forum: General Software Library (https://www.hpmuseum.org/forum/forum-13.html)
+--- Thread: (49G) Moser de Bruijn Sequence, OEIS A000695 (/thread-5040.html)



(49G) Moser de Bruijn Sequence, OEIS A000695 - Gerald H - 2015-10-30 12:36

For integer input N the programme returns the Nth element of the sequence

http://oeis.org/A000695

ie the binary base representation of N read as a quaternary base number.

Code:
::
  CK1&Dispatch
  # FF
  ::
    FPTR2 ^DupQIsZero?
    ?SEMI
    Z0_
    SWAP
    FPTR2 ^Z>ZH
    FPTR2 ^ZBits
    ZERO_DO
    SWAP
    Z4_
    FPTR2 ^RMULText
    SWAP
    ISTOP-INDEX
    #1-
    FPTR2 ^ZBit?
    IT
    ::
      SWAP
      Z1_
      FPTR2 ^RADDext
      SWAP
    ;
    LOOP
    DROP
  ;
;



RE: (49G) Moser de Bruijn Sequence, OEIS A000695 - Thomas Klemm - 2025-10-19 22:27

This program is for the HP-42S:
Code:
00 { 26-Byte Prgm }
01 STO 00
02 2
03 X<>Y
04▸LBL 00
05 4
06 X<>Y
07 2
08 ÷
09 IP
10 X=0?
11 GTO 01
12 R↓
13 ×
14 R↓
15 ×
16 STO+ 00
17 R↓
18 GTO 00
19▸LBL 01
20 RCL 00
21 END

But it can also be used with most other models.

Examples

13 R/S
81

44 R/S
1104

References

To use these program set:

4 STO 1
2 STO 2



RE: (49G) Moser de Bruijn Sequence, OEIS A000695 - John Keith - 2025-10-22 08:21

Ah, one of my favorite sequences! Smile  It has a simple fractal structure: if k appears in the sequence, so do 4k and 4k+1. This leads to a fast and simple program which, given n on the stack, returns a list of the first 2^n terms. For 48G and later (replace NIP with SWAP DROP for the 48).

Code:
\<< 1 { 0 1 } ROT 2 SWAP
  START SWAP 4 * SWAP DUP2 ADD +
  NEXT NIP
\>>