Number division leading to even number

50 Views Asked by At

I am facing a problem where I have to pick an even integer number X in the vicinity of another number Z (say X>=Z). Now I would like to generate a sequence of numbers according to

Y(i) = X(i)/2 + 2

X(i) = Y(i)

and loop over this for some given number of times (say n). How do I pick X(0) such that Y(i)s are even numbers?

1

There are 1 best solutions below

5
On BEST ANSWER

You can work backwards. Suppose that, for a fixed $n$, you choose $x_n$ to be an even number. Then:

$$ \begin{align} x_{n-1} & = 2 x_n - 4 \\ x_{n-2} & = 2 x_{n-1} - 4 \\ \cdots \\ x_1 & = 2 x_2 - 4 \\ x_0 & = 2 x_1 - 4 \end{align} $$

Multiplying each equation $x_i = 2 x_{i+1}-4$ by $2^i$ and adding all together telescopes to:

$$ x_0 = 2^n\,x_n - 4\,(2^n-1) $$

This gives the starting value $x_0$ which results in $x_n$ after $n$ iterations, and the relations above ensure that all $x_i$ in between are even integers.