Alternative to Ceiling Function for Simple Sequence

204 Views Asked by At

I came across a problem where I was wondering if there is a simple expression to map a sequence of numbers to the same adjacent values without using a ceiling function.

For instance, given the sequence:

{1, 2, 3, 4, 5, 6} -> {1, 1, 2, 2, 3, 3}

My solution would be $ f(x) = \left \lceil \frac{x}{2} \right \rceil $

Is there an alternative form that would not require using the ceiling or floor functions, perhaps using the mod operator instead?

1

There are 1 best solutions below

0
On BEST ANSWER

Since $f:\{1,2,3,4,5,6,\cdots\}\mapsto\{1,1,2,2,3,3,\cdots\}$

we see that

\begin{eqnarray} f(n)+f(n+1)&=n+1\tag{1}\\ -f(n)+f(n+1)&=\frac{1+(-1)^n}{2}\tag{2} \end{eqnarray}

Subtracting equation $(2)$ from equation $(1)$ and solving for $f(n)$ gives

$$ f(n)=\frac{2n+1-(-1)^n}{4} $$