Algorithm to find the next number in a sequence
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Predicting the next value in a sequence looks simple until you ask what rule produced the numbers. With only a few terms, many different rules can fit the same data, so a good algorithm starts by identifying what kinds of patterns it is willing to recognize.
Why a Sequence Alone Is Not Enough
There is no universal algorithm that can always infer the "correct" next number from a finite list. For example, the sequence 2, 4, 6, 8 could continue with 10 if it is arithmetic, or it could switch to 16 if the hidden rule was "double until the fourth term, then square the next one." Both rules match the observed data.
In practice, sequence solvers use assumptions. Common assumptions include:
- the sequence is arithmetic
- the sequence is geometric
- the sequence is generated by a low-degree polynomial
- the sequence follows a small recurrence based on earlier terms
That means the real problem is not "guess anything," but "detect the simplest plausible rule from a known family."
Practical Heuristics for Common Sequences
The first pass is usually cheap:
- Check whether consecutive differences are constant. If they are, the sequence is arithmetic.
- Check whether consecutive ratios are constant and the terms are nonzero. If they are, the sequence is geometric.
- Check whether the sequence depends on the previous one or two terms, such as Fibonacci-style recurrences.
Here is a simple Python function that detects arithmetic and geometric sequences:
This works well for the easy cases, but it intentionally refuses to guess when the pattern is unclear.
Finite Differences for Polynomial Sequences
A more powerful technique is finite differences. If the first differences are constant, the sequence is linear. If the second differences are constant, it is quadratic. Constant third differences suggest a cubic rule, and so on.
For example:
- sequence:
1, 4, 9, 16 - first differences:
3, 5, 7 - second differences:
2, 2
Because the second differences are constant, the sequence comes from a quadratic polynomial, and the next first difference should be 9, giving the next term 25.
The following implementation predicts the next term when the data is consistent with a low-degree polynomial:
This method is useful, but it has an important limitation: any finite list of values can be fit by some polynomial. If you allow arbitrarily high degree, the algorithm will always produce a next value even when that guess is meaningless.
Choosing a Sensible Strategy
A robust sequence predictor usually follows a layered approach:
- Try arithmetic and geometric rules first.
- Try low-degree polynomial rules with finite differences.
- Try a small recurrence if the sequence appears to depend on prior terms.
- If several models fit, prefer the simplest one and report that the answer is an assumption, not a proof.
That last step matters. In interview problems or puzzle sites, there is often an intended pattern. In production software, you should avoid presenting a speculative continuation as fact.
Common Pitfalls
The biggest mistake is assuming every sequence has one objectively correct continuation. It does not. A short list of numbers almost never contains enough information to prove a unique rule.
Another common issue is overfitting. A polynomial of high enough degree can match every observed term and still generalize badly. Restrict the search to simple families such as arithmetic, geometric, or low-degree polynomial rules.
Ratio-based detection also breaks when zero appears in the sequence. If you divide by a prior term without checking for zero, the algorithm fails. Handle that case separately.
Finally, be careful about integer-only assumptions. Some sequences naturally produce fractions, and using integer division can silently corrupt the result.
Summary
- There is no general algorithm that can always infer the true next number from a finite sequence.
- Practical solutions work by testing a limited family of patterns.
- Arithmetic and geometric checks are cheap and should come first.
- Finite differences are effective for low-degree polynomial sequences.
- Good sequence predictors should report assumptions instead of pretending the answer is guaranteed.

