Finding a function for a sequence that alternates between $0$ and $1$ $\frac{n}{2}$ times?

828 Views Asked by At

I should probably have figured this out by now, but I'm exhausted and I've spent way too long on such a small part of a larger problem.

I'm trying to find a function that alternates between two numbers (for simplicity I've chosen $0$ and $1$) repeating each number $\frac{n}{2}$ times. With it's "cycle length" being $c$. So, it repeats every $n$th number. For example, a function with a cycle length of $10$ would go: $\left\{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1...\right\}$

How would one go about constructing a function for a cycle of length $n$?

2

There are 2 best solutions below

1
On BEST ANSWER

$$\left\lfloor \frac{2k}{n} \right\rfloor \bmod 2, \quad k=0, 1, 2, 3,\ldots$$ where $n$ is your (even) cycle length. The quotient is simplier when using the cycle half-length $c=\frac{n}{2}$: $$\left\lfloor \frac{k}{c} \right\rfloor \bmod 2, \quad k=0, 1, 2, 3,\ldots$$

0
On

$$ f(x)=\left\lfloor \frac{x}{\dfrac{n}{2}} \right\rfloor \bmod 2, \quad x=0, 1, 2, 3,\ldots$$ Here n is the total length of the period.

Thankyou @madnessweasley for the insight. I was groping with the pulse function. Nicely pointed. And of course the presenter of this OP for such a beautiful yet simple question.