Recamán's sequence (A005132) is a mathematical sequence, defined as such:
$$A(n) = \begin{cases}0 & \textrm{if } n = 0 \\ A(n-1) - n & \textrm{if } A(n-1) - n \textrm{ is positive and not already in the sequence} \\ % Seems more readable than %A(n-1) - n & \textrm{if } A(n-1) > n \wedge \not\exists m < n: A(m) = A(n-1)-n \\ A(n-1) + n & \textrm{otherwise} \end{cases}$$
An alternative, simpler verbal explanation is as follows:
Subtract unless you can't (the number is negative, or has been used before), in which case add.
The first few terms are \$0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11\$
Now, there is already this challenge which asks you to generate the n
th term of the sequence. This one is slightly different.
Challenge
Given a number n
, draw the first n
terms of the sequence. What do I mean by 'draw'? Let me demonstrate:
- Draw a number line
max([A(y) for y<=n])
units long. We'll assumen
is 5, for now, so the number line is 6 units long (since the largest of \$A(1) = 0\$, \$A(2) = 1\$, \$A(3) = 3\$, \$A(4) = 6\$ and \$A(5) = 2\$ is \$6\$). Make the line from underscores, starting at 0:
______
- Start with the transition between the first and second terms: that is, 0 and 1. Use
|
and-
to draw a square (equal length and height), going upwards. In this case, we'll have to miss out the-
because the distance is only 1.
||
______
- Now, we'll draw on the next step (\$A(2) = 1\$ to \$A(3) = 3\$) on the bottom of the line (we alternate between up and down each time):
||
______
| |
|-|
As you can see, this line also has a height of 2, since the height must be equal to the distance between the two terms.
If we continue, we will eventually get to:
|--|
| |
|| | |
______
||| |
||| |
| |
|---|
Rules
- If there is a
-
and|
colliding, the later one takes priority. - There may be preceeding/trailing spaces before/after the image, but trailing/preceeding
_
s or-
s are not allowed (exception is 0- or 1- indexing) - You can choose to set the 0 point just before the first
_
on the number line, or just after it. - No alternative characters for
-
,|
or_
may be used. - This is code-golf, so shortest answer in bytes wins.
Test case
Here is another test case, with n=10
|-------|
||-----||
|| ||
|----| || ||
| | || ||
||--|| || ||
|| || || ||
|||| || || ||
_____________________
||| || ||| ||
||| || ||| ||
| || ||| ||
|---|| ||| ||
| ||| ||
|---||| ||
||------||
|--------|
As you can see, this line also has a height of 2, since the height must be equal to the distance between the two terms.
, as well asYou can choose to set the 0 point just before the first _ on the number line, or just after it.
wrap this up pretty well. \$\endgroup\$