# Strange behaviour if FindSequenceFunction

Consider a sequence starting with 2, subtract 2 to get the next term, now add 3 to get the next term and so on according to the rule (-2,+3) consecutively which starts like

s = {2, 0, 3, 1, 4, 2, 5, 3, 6, 4};


and ask for the sequence function:

FindSequenceFunction[s, n] // Simplify


$\frac{3 \left(894-909 n+307 n^2-42 n^3+2 n^4\right)}{2 \left(579-539 n+170 n^2-22 n^3+n^4\right)}$

This strange looking formula is in fact the wrong alternative, as the next term should be 4 + 3 = 7. Instead it repdocuces just the given terms an gives fractions beyond:

Table[%, {n, 0, 20}]

(* Out[94]= {447/193, 2, 0, 3, 1, 4, 2, 5, 3, 6, 4, 711/193, 1545/437, 968/281, 1654/489, \
377/113, 571/173, 2490/761, 24504/7543, 3723/1153, 44757/13933} *)


Let us now drop the last element of s:

FindSequenceFunction[Drop[s, 1], n] // Simplify
(* Out[72]= 1/4 (3 + 5 (-1)^n + 2 n) *)

Table[1/4 (3 + 5 (-1)^n + 2 n), {n, 0, 10}]
(* Out[80]= {2, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7} *)


This formula is correct.

The same holds if we append one term to s

FindSequenceFunction[Join[s, {7}], n] // Simplify
(* Out[74]= 1/4 (1 - 5 (-1)^n + 2 n) *)

Table[1/4 (1 - 5 (-1)^n + 2 n), {n, 1, 10}]
(* Out[82]= {2, 0, 3, 1, 4, 2, 5, 3, 6, 4} *)


Question: is there a general rule for the reliability of the result of FindSequenceFunction[]?

Regards,
Wolfgang

• Isn't it that any finite sequence has an infinite amount of possible rules? So what is meant by reliable? Dec 12, 2014 at 14:32
• You are right. But MMA found a "wrong" result with k terms and the "right" one with k-1 and k+1 terms. Perhaps there's a better question resulting from my observation. For instance, the natural requirement that all terms must be integers also beyond the given ones. Maybe this is already sufficient ...? Yes, I think so! Dec 12, 2014 at 15:22
• Alternative ... oeis.org/wiki/User:Enrique_P%C3%A9rez_Herrero/OEIS_Package Dec 12, 2014 at 16:51
• @Dr.WolfgangHintze Hmmm, I don't. There should still be an infinite amount of infinite integer series that contain the given finite sequence. Dec 12, 2014 at 18:16
• @ belisarius: thanks for the interesting link Dec 12, 2014 at 21:17

\$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global*"]


Vary the ValidationLength and take the most common result.

s = {2, 0, 3, 1, 4, 2, 5, 3, 6, 4};

(candidates = FullSimplify[FindSequenceFunction[s, n,
ValidationLength -> #] & /@
Range[0, Length[s] - 5]]) // Column[#, Frame -> All] &


f[n_] = MaximalBy[Last][Tally[candidates]][[1, 1]]

1/4 (-1)^n (-5 + (-1)^n (1 + 2 n))


Verifying that f generates the original sequence

test = f /@ Range[17]

(* {2, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8, 6, 9, 7, 10} *)

test[[1 ;; Length@s]] === s

(* True *)


Looking at the difference pattern for f

Differences[test]

(* {-2, 3, -2, 3, -2, 3, -2, 3, -2, 3, -2, 3, -2, 3, -2, 3} *)


Comparing with RSolve

f2[n_] = RSolveValue[{a[n] == a[n - 2] + 1, a[1] == 2, a[2] == 0}, a[n], n] //
FullSimplify

(* 1/4 (1 - 5 (-1)^n + 2 n) *)

Assuming[n ∈ Integers, f[n] == f2[n] // Simplify]

(* True *)
`