The Method of Finite Differences is a technique used to find the next term in a sequence of numbers, given that the sequence is governed by consecutive values of a certain polynomial. Given a list of n
terms, the Method will be able to determine the degree of the polynomial, which is a maximum of n+1
.
The Method works by arranging the numbers in order, like so.
4 8 15 16 23 42
Then the difference between any number and its neighbor to the right (given that it has a neighbor to the right) is written in the row below. Note that entries must be written one space (ASCII 20) to the right of the longest entry in the previous column.
4 8 15 16 23 42
4 7 1 7 19
The process is repeated until there is one entry left in the bottom row.
4 8 15 16 23 42
4 7 1 7 19
3 -6 6 12
-9 12 6
21 -6
-27
Then the bottom row's entry is duplicated.
4 8 15 16 23 42
4 7 1 7 19
3 -6 6 12
-9 12 6
21 -6
-27 -27
Then the above process for finding differences is reversed until there is a new entry in the first row, which is separated from the other entries by |
4 8 15 16 23 42 | 46
4 7 1 7 19 4
3 -6 6 12 -15
-9 12 6 -27
21 -6 -33
-27 -27
The Method moves down rows until all of the entries in a row are equal. Take the starting numbers 2 5 10 17 26
. This is the finished table.
2 5 10 17 26 | 37
3 5 7 9 11
2 2 2 2
The degree of the polynomial is the number of rows minus 1. As you might tell from the previous example, the polynomial is x^2+1
.
Your program will take as its input a list of integers separated by whitespace. You may assume that the list contains only integers and has at least 2 elements (they need not be distinct; the resulting chart would have only 1 row). The program will output the entire table generated by the Method of Finite Differences, as defined above.
This is code golf. Standard rules apply. Shortest code in bytes wins.