14
\$\begingroup\$

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 nth 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:

  1. Draw a number line max([A(y) for y<=n]) units long. We'll assume n 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:

______

  1. 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.
||
______
  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 , so shortest answer in bytes wins.

Test case

Here is another test case, with n=10

            |-------|
            ||-----||
            ||     ||
  |----|    ||     ||
  |    |    ||     ||
  ||--||    ||     ||
  ||  ||    ||     ||
||||  ||    ||     ||
_____________________
 |||  ||   |||     ||
 |||  ||   |||     ||
  |   ||   |||     ||
  |---||   |||     ||
       |   |||     ||
       |---|||     ||
           ||------||
           |--------|
\$\endgroup\$
7
  • \$\begingroup\$ It is not clear where left edge of square should be placed. \$\endgroup\$ Commented Jul 18, 2019 at 16:55
  • \$\begingroup\$ @DaniilTutubalin I'm not sure I understand what you mean. \$\endgroup\$ Commented Jul 18, 2019 at 16:56
  • \$\begingroup\$ basically, statement only specifies that we need to draw squares (width = height) and that they should alternate between up and down. There are no instructions about squares' size and position. In test case I see that 2 squares may have the same position of left edge. \$\endgroup\$ Commented Jul 18, 2019 at 17:03
  • \$\begingroup\$ I think 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 as You 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\$ Commented Jul 18, 2019 at 17:05
  • \$\begingroup\$ I think the test case for n=10 is wrong from 13-> 20 onwards. \$\endgroup\$ Commented Jul 18, 2019 at 21:41

1 Answer 1

5
\$\begingroup\$

Jelly, 70 65 bytes

µ_ż+ɗLṪ>Ƈ-ḟ⁸Ḣṭµ¡µL;ⱮṀ’ṭr,;ɗƝJW,RN¹ƭƲ€$+Lp""ƲZẎ€Ʋ‘ŒṬ×"J$»/ị“-|_ ”Y

Try it online!

A full program taking a single integer \$n\$ via STDIN and outputting the ASCII art based on zero-indexing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.