2
$\begingroup$

Given $n \in \Bbb N$ | $n \gt 0$, I want to map an incrementing input number to a periodic sequence.

$$ \left[ \begin{array}{c|cc} x&0&1&2&...&...&...&...&...&...&...&i-1&i\\ y&0&1&2&...&n-1&n&n-1&...&1&0&1&2 \end{array} \right] $$

For $n=2$ it'd look like $$ \left[ \begin{array}{c|cc} x&0&1&2&3&4&5&6&7&8\\ y&0&1&2&1&0&1&2&1&0 \end{array} \right] $$

This reminds me of a periodic zigzag sequence (https://oeis.org/A007877), but I can't come up with a general equation for a variable $n$.

$\endgroup$

1 Answer 1

1
$\begingroup$

Try $y = n-| (x \bmod 2n) - n|$.

$\endgroup$
1
  • $\begingroup$ Looks good to me, thank you! $\endgroup$
    – manniL
    Dec 13, 2017 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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