# Simplify the use of Table together with FoldList

I am trying to recreate the following table,where you have the question mark.

to do it occupy the following commands, but I would like to help me improve my code to make it more practical especially where I use Table together with FoldList.

x={1,2,4,7,11,16,22,29,37,46};y={1,3,6,10,15,21,28,36,45,55};

sums=Outer[Plus,Differences[x],Differences[y]];

fin=Table[FoldList[Plus,x[[k+1]],sums[[k]]], {k,1,9}];

TableForm[Transpose[fin]]


• The complete table would be Grid[tab = Transpose[Join[{y}, fin]], Frame -> All] Commented Jun 22, 2018 at 21:02
• an alternative way to get fin: fin2 =Table[Accumulate@Prepend[sums[[k]], 0] + x[[k + 1]], {k, 1, 9}].
– kglr
Commented Jun 22, 2018 at 21:14
• Are you interested in using these specific methods for solving this problem, or are you open to alternative approaches (such as an algebraic closed form for $F(x,y)$)? If you're going for practical code, I find that such closed forms are usually close to optimal, especially for space usage. Commented Jun 22, 2018 at 21:15
• @eyorble Hello, I find the proposal of using closed algebraic forms interesting, could you give an example for this thread? Commented Jun 22, 2018 at 22:26
• @kglr Your code is interesting, since you use the Accumulate command, which had not crossed my mind, once again I see that your help gives new approaches to solve my doubts. Commented Jun 22, 2018 at 22:31

Starting with table as input you can use FindSequenceFunction iteratively to find the function f0[table][x,y] that generates table:

ClearAll[f0]
f0[tab_][x_, y_] := FindSequenceFunction[FindSequenceFunction[#][y] & /@ tab][x]


Example:

table // Grid[#, Dividers -> All]& // TeXForm


$\begin{array}{|c|c|c|c|c|c|c|c|c|c|} \hline 1 & 2 & 4 & 7 & 11 & 16 & 22 & 29 & 37 & 46 \\ \hline 3 & 5 & 8 & 12 & 17 & 23 & 30 & 38 & 47 & 57 \\ \hline 6 & 9 & 13 & 18 & 24 & 31 & 39 & 48 & 58 & 69 \\ \hline 10 & 14 & 19 & 25 & 32 & 40 & 49 & 59 & 70 & 82 \\ \hline 15 & 20 & 26 & 33 & 41 & 50 & 60 & 71 & 83 & 96 \\ \hline 21 & 27 & 34 & 42 & 51 & 61 & 72 & 84 & 97 & 111 \\ \hline 28 & 35 & 43 & 52 & 62 & 73 & 85 & 98 & 112 & 127 \\ \hline 36 & 44 & 53 & 63 & 74 & 86 & 99 & 113 & 128 & 144 \\ \hline 45 & 54 & 64 & 75 & 87 & 100 & 114 & 129 & 145 & 162 \\ \hline 55 & 65 & 76 & 88 & 101 & 115 & 130 & 146 & 163 & 181 \\ \hline \end{array}$

f0[table][x, y]


1/2 (2 - x + x^2 - 3 y + 2 x y + y^2)

Column[{Style[TraditionalForm[F[x, y] == f0[table][x, y]], 14],
Grid[Array[f0[table], Dimensions @ table], Dividers -> All]},
Alignment -> Center] // TeXForm


$\begin{array}{c} F(x,y)=\frac{1}{2} \left(x^2+2 x y-x+y^2-3 y+2\right) \\ \begin{array}{|c|c|c|c|c|c|c|c|c|c|} \hline 1 & 2 & 4 & 7 & 11 & 16 & 22 & 29 & 37 & 46 \\ \hline 3 & 5 & 8 & 12 & 17 & 23 & 30 & 38 & 47 & 57 \\ \hline 6 & 9 & 13 & 18 & 24 & 31 & 39 & 48 & 58 & 69 \\ \hline 10 & 14 & 19 & 25 & 32 & 40 & 49 & 59 & 70 & 82 \\ \hline 15 & 20 & 26 & 33 & 41 & 50 & 60 & 71 & 83 & 96 \\ \hline 21 & 27 & 34 & 42 & 51 & 61 & 72 & 84 & 97 & 111 \\ \hline 28 & 35 & 43 & 52 & 62 & 73 & 85 & 98 & 112 & 127 \\ \hline 36 & 44 & 53 & 63 & 74 & 86 & 99 & 113 & 128 & 144 \\ \hline 45 & 54 & 64 & 75 & 87 & 100 & 114 & 129 & 145 & 162 \\ \hline 55 & 65 & 76 & 88 & 101 & 115 & 130 & 146 & 163 & 181 \\ \hline \end{array} \\ \end{array}$

Array[f0[table], Dimensions @ table] == table


True

This is the same function as @eyorble's f:

Simplify[f0[table][x, y] == f[x, y]]


True

• Thanks for helping me, I did not know the FindSequenceFunction command and much less wise that it could be used iteratively as you propose, that trick was great. I never thought that I would go from the construction of the table until I found the generating function of said table, I only wanted to recreate it in MMA, but thanks to the help of the community I learned more. Thanks again Commented Jun 23, 2018 at 21:56
• @bullitohappy, my pleasure. Glad you found it useful.
– kglr
Commented Jun 24, 2018 at 10:09

The right hand $F(x,y)$ has the simple closed form of:

f[x_,y_] := 1/2 (x+y-1) (x+y-2) + x;


This can be verified by running:

Table[f[x,y], {x, 1, 10}, {y, 1, 10}] // MatrixForm


The derivation of this form mostly stems from intuition, but it becomes clear if the row-number is subtracted from each element:

Table[f[x,y] - x, {x, 1, 10}, {y, 1, 10}] // MatrixForm


It turns out that it is a simple table (in terms of x) added onto the triangular numbers (in terms of x+y).

Most tables where the value strictly increases away from the origin can be rewritten in terms of x+y, and they may sometimes become simpler to intuit that way.

• Thanks for supporting me and for your excellent explanation. It is a subject that I did not think about, when I started to do the code to recreate the table in question. Commented Jun 23, 2018 at 21:47