2
$\begingroup$

Can we define in advance that $k$ is even?

For example, when defining the sequence $\{a_n\}$ inductively as follows,

input:

a[n_]:=If[Mod[Abs[a[n-1]-a[n-2]],2]==0,a[n-1]-a[n-2],a[n-1]-2];
   a[1] = 3; a[2] = k; 

We would like to define $k$ in advance as an even number.

$\endgroup$
0

1 Answer 1

3
$\begingroup$

This is probably most easily accomplished as follows

$Assumptions = {khalf \[Element] Integers}
a[n_] := a[n] = FullSimplify[
    If[Mod[Abs[a[n - 1] - a[n - 2]], 2] == 0, 
       a[n - 1] - a[n - 2], 
       a[n - 1] - 2]];
a[1] = 3;
a[2] = 2 khalf

Note that

  • I memoized a[n] to save on time.
  • I wrapped the evaluation in FullSimplify to trigger the application of the $Assumptions.
  • I changed a[2] to twice khalf and put into the $Assumptions that khalf is an integer.

Then

a[3] == 2(-1 + khalf)
a[4] == -2
a[5] == -2 khalf
a[6] == 2 - 2 khalf

and so on.

$\endgroup$
2
  • $\begingroup$ It worked out well. thank you!! $\endgroup$
    – Young
    Commented Dec 3 at 4:06
  • $\begingroup$ I'm glad it helped! You're very welcome! $\endgroup$
    – evanb
    Commented Dec 3 at 19:55

Your Answer

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

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