Recaman’s sequence

a[0]:0;
recaman:set(0);
recaman_next(n):=block([x:a[n-1]-n],
if (x > 0) and not elementp(x, recaman) then
( a[n]:x, recaman:adjoin(x, recaman))
else ( a[n]:a[n-1]+n, recaman:adjoin(a[n], recaman)));
for i:1 thru 30 do (recaman_next(i), print(i,"=",a[i]));

1 = 1
2 = 3
3 = 6
4 = 2
5 = 7
6 = 13
7 = 20
8 = 12
9 = 21
10 = 11
11 = 22
12 = 10
13 = 23
14 = 9
15 = 24
16 = 8
17 = 25
18 = 43
19 = 62
20 = 42
21 = 63
22 = 41
23 = 18
24 = 42
25 = 17
26 = 43
27 = 16
28 = 44
29 = 15
30 = 45

Reference is OEIS A005132

Leave a comment

Your email address will not be published. Required fields are marked *