I would appreciate it if somebody could help me with the following problem:

Given $$a_{n+1}=\begin{cases} \dfrac{a_n}{2}& (a_n \: \text{even})\\ n{a_n}& (a_n \: \text{odd})\end{cases},\:a_8= 10$$ we want to get all possible values with $a_1$. How can I get it using Mathematica11? Please.

  • $\begingroup$ You can represent this without needing piecewise: as $a_{n+1}=\frac{1}{2} a_n \left((2 n-1) (a_n \bmod 2)+1\right)$ or even without the mod, as $\frac{1}{4} a_n \left((1-2 n) \cos \left(\pi a_n\right)+2 n+1\right)$ $\endgroup$
    – flinty
    Dec 28, 2022 at 12:21

2 Answers 2


We could invert the functions and go "backwards". So, a[n-1] could be 2*a[n] if 2*a[n] is even (which it will always be, so we always have at least one possibility). But also, a[n-1] could be a[n]/(n-1) if that value is odd.

So, given a value for an index, we can compute possible previous values:

PreviousAs[{idx_Integer, value_Integer}] :=
    {evenCase = 2 value, oddCase = value/(idx - 1)},
    Thread[{idx - 1, {evenCase, If[OddQ[oddCase], oddCase, Nothing]}}]]

We're given a value for index 8, and we encode this as {8,10}. We want to go backwards 7 steps, so evaluate this to get the possible values for index 1:

Nest[Catenate@*Map[PreviousAs], {{8, 10}}, 7]

We can test that it works (although this test doesn't guarantee that we found them all--maybe my logic above was incomplete) by defining the "forward" function:

NextA[{idx_Integer, value_Integer?EvenQ}] := {idx + 1, value/2};
NextA[{idx_Integer, value_Integer?OddQ}] := {idx + 1, idx value}

and iterating on one of the candidates generated above:

Nest[NextA, <...copy/paste one of the candidates here...>, 7]

Hopefully we get {8,10} back.

  • 1
    $\begingroup$ The logic is fine. It seems that $a_8=10$ is not very interesting. For $a_8 = 21$, one can use your code Nest[Catenate@*Map[PreviousAs],{{8,21}},7] to get a more interesting solution {{1,2688},{1,224},{1,192},{1,24},{1,6},{1,3},{1,4}} . $\endgroup$
    – user293787
    Dec 28, 2022 at 9:47

We first write the recurrence equation like: a[n_]:= ... and set a[1]=a1 where a1 is not yet known:

a[1] = a1;
a[n_] := a[n] = If[OddQ[n], a[n - 1]/1, a[n - 1] (n - 1)];

Now we can get a[8]:

(* 105 a1 *)

From this we see that a1= 10/105:


To test:

(* 10 *)

and the whole sequence up to 10:

(* {2/21, 2/21, 2/21, 2/7, 2/7, 10/7, 10/7, 10, 10, 90} *)
  • $\begingroup$ Similar observation as per @user293787 to my answer - the as need to be integers. $\endgroup$ Dec 30, 2022 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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