0
$\begingroup$

As part of a code that I want to implement more efficiently, I would like to find a closed form expression for the following sequence of length $N+L-1$ (the elements of the sequence are separated by comma):

$1,\, 2,\, 3,\, \ldots N-1,\, N,\, N,\, \ldots N,\, N,\, N-1,\, \ldots 3,\, 2,\, 1$

I have tried to find a relationship between the position of each number and its value by subtracting by some numbers, taking absolute values, dividing by a number and taking the integer part, but so far I haven't been able to get a closed form expression. Any hint or answer would be appreciated.

By closed form expression, I mean a formula that gives the value of the $k$-th term in the sequence given the positive integers $L$ and $N$. I am looking for an expression that is not defined by intervals, but that works for any value of $k$.

$\endgroup$
5
  • $\begingroup$ What is L? {}{} $\endgroup$
    – Szeto
    Commented Jul 13, 2018 at 15:05
  • $\begingroup$ You mean you want a formula to describe the $k$th position in the sequence for $N$? Or are you treating that whole string as an integer and you want a formula $f(L,N)$? $\endgroup$
    – Randall
    Commented Jul 13, 2018 at 15:05
  • $\begingroup$ @Randall and Szeto, I have added additional information to my question. Hopefully, is clearer now. Thanks. $\endgroup$ Commented Jul 13, 2018 at 15:22
  • $\begingroup$ Do you really need a formula? What is the programming motivation for this, vectorization perhaps? $\endgroup$
    – Ian
    Commented Jul 13, 2018 at 15:59
  • 1
    $\begingroup$ At any rate, look at min(N,L/2-|x|) (and try to determine what the appropriate notion of /2 is). $\endgroup$
    – Ian
    Commented Jul 13, 2018 at 16:01

1 Answer 1

2
$\begingroup$

Let's start with the case $L \ge N$ so you do get a run of $N$s in the middle. You have a run of $N-1$ items from $1$ to $N-1$ at the start, a run of $N-1$ items at the end, so $N+L-1-(2N-2)=L-N+1\ N$'s in the center run. This suggests $$f(k)=\begin {cases} k&1 \le k \le N-1\\N&N\le k \le L\\L+N-k&k \gt L \end {cases}$$ If $L \lt N$ the run stops before it gets to $N$. The top number is $\frac 12(N+L-1)$, rounded up if the length is odd. If it is odd you have just one high number in the middle, while if it is even you have two. This suggests $$f(k)=\begin {cases} k&k\le \frac 12(N+L-1)\\N+L-k&k \ge \frac 12(N+L-1) \end {cases}$$

$\endgroup$
2
  • $\begingroup$ Is it possible to have an expression that works for any value of $k$? That is, that is not defined for intervals? The point of wanting a closed form expression is to avoid conditionals in my code. $\endgroup$ Commented Jul 13, 2018 at 15:27
  • $\begingroup$ You can do that, but it is really just hiding the fact that you have conditionals.. You can write the second using the absolute value of the distance from the center. It takes a bit of chasing the $+1$s to get the exact expression. You can write the first the same way letting the center go up to $\frac 12(N+L-1)$ and then taking the min of $f(k)$ and $N$. $\endgroup$ Commented Jul 13, 2018 at 16:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .