Calculating and plotting the Recamán's sequence

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
PKeller
Posts: 5
Joined: Fri Sep 25, 2020 8:22 am

Calculating and plotting the Recamán's sequence

Post by PKeller » Sun Oct 04, 2020 3:05 pm

Recamán's sequence

RecN is just a simple program to calculate the n-th term of the Recamán sequence. The sequence is defined as:

\(
a_n=\begin{cases} 0&\text{if }n=0\\a_{n-1}-n&\text{if }a_{n-1}-n>0;\text{ and not already in the sequence;}\\a_{n-1}+n&\text{otherwise.}\end{cases}
\)


The program RecN takes as input "n" in the X-Stack and then outputs the maximal term of the sequence \(a_0\ldots a_n\) in the Y-Stack and \(a_n\) in the X-Stack. It uses 200 registers (STO 00 to STO 199) to store the information if a term \(a_k\) is already in the sequence.

The program RecP is a simple plot program that visualizes the first n terms of the Recamán sequence using alternating semi-circles, that had been shown on the Numberphile YouTube channel in a video titled The Slightly Spooky Recamán Sequence. It uses as a sub-program CIRC, which plots semi-circles. It requires as input an integer n in the X-Stack.

Code: Select all

00 { 247-Byte Prgm }
01▸LBL "RecN"
02 STO "n"
03 CLRG
04 SIZE 300
05 0
06 STO "a"
07 0
08 STO "RMAX"
09 1
10 STO 00
11 RCL "n"
12 X=0?
13 GTO 09
14 0
15 STO "I"
16▸LBL 01
17 1
18 STO+ "I"
19 RCL "a"
20 STO "b"
21 RCL "I"
22 -
23 X>0?
24 GTO 04
25▸LBL 02
26 RCL "b"
27 RCL "I"
28 +
29 STO "a"
30▸LBL 03
31 RCL "a"
32 32
33 ÷
34 IP
35 STO "BINI"
36 RCL "a"
37 32
38 MOD
39 STO "BINF"
40 2
41 RCL "BINF"
42 Y↑X
43 STO+ IND "BINI"
44 RCL "RMAX"
45 RCL "a"
46 X>Y?
47 STO "RMAX"
48 RCL "I"
49 RCL "n"
50 X>Y?
51 GTO 01
52 GTO 09
53▸LBL 04
54 RCL "b"
55 RCL "I"
56 -
57 32
58 ÷
59 IP
60 STO "BINI"
61 RCL "b"
62 RCL "I"
63 -
64 32
65 MOD
66 STO "BINF"
67 RCL IND "BINI"
68 RCL "BINF"
69 BIT?
70 GTO 02
71 RCL "b"
72 RCL "I"
73 -
74 STO "a"
75 GTO 03
76▸LBL 09
77 RCL "RMAX"
78 RCL "a"
79 CLV "n"
80 CLV "BINI"
81 CLV "BINF"
82 CLV "a"
83 CLV "b"
84 CLV "I"
85 CLV "RMAX"
86 END
Here is a plot of the RecP program of the first 28 terms:
20201004-14550314.bmp
First 28 terms
20201004-14550314.bmp (12.31 KiB) Viewed 143 times
and here of the first 2'000 terms
20201004-05131233.bmp
First 2'000 terms
20201004-05131233.bmp (12.31 KiB) Viewed 143 times
And here
recaman.raw
Programs RecN, RecN and CIRC
(687 Bytes) Downloaded 9 times
is the raw file of the three programs.

Post Reply