# Dynamic programming: Fibonacci generator

I have the following piece of code:

A[m_, k_] := For[i = 1, i <= m, i++,
For[j = 1, j <= k, j++,
A[i_, j_] = If[i <= 0, 0,
If[[i == 1 || i == 2] && j == 2, 1,
If[OddQ[i]; j == 1, 1,
If[! OddQ[i]; j == 1, 0,
If[j == 2, A[i - 2, 2] + A[i - 3, 2],
If[i <= j, Fibonacci[i],
False]
]
]
]
]
]
]
]


When executing A[2, 3] I should get an answer that is equal to Fibonacci, but it does not work that way. Any suggestions ?

• Real programmers don't use loops – Dr. belisarius Aug 3 '15 at 19:03
• Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Aug 3 '15 at 19:03
• Not sure what you're trying to achieve here (and I'm sure there are better ways) but the problem may lie in your usage of i, j both as symbolic patterns in the second definition of A and as variables with values in the outer loop. It might be that removing the underscores in the second definition resolves the problem. – Sjoerd C. de Vries Aug 3 '15 at 19:21
• Actually, what I actually need is solving a recursive formula. Need to perform this without a recursion, for complexity reasons. And thus have this frustrating code. Is there a simple way to do this? (with two cases in the formula) – shi Aug 3 '15 at 20:47
• @shi So you may post your formula ... – Dr. belisarius Aug 3 '15 at 20:50

Ok, so this is the solution:

A[m_, k_] := For[i = 1, i <= m, i++,
For[j = 1, j <= k, j++,
A[i, j] =
If[i <= 0, 0,
If[(i == 1 || i == 2) && j == 2, 1,
If[OddQ[i]; j == 1, 1,
If[! OddQ[i]; j == 1, 0,
If[j == 2, A[i - 2, 2] + A[i - 3, 2],
If[i <= j, Fibonacci[i],
False]]]]]]]]


I found an error in one of the If statements:

[i == 1 || i == 2] && j == 2


should be

(i == 1 || i == 2) && j == 2


I subsequently changed

A[i_, j_] =


to

A[i, j] =


in this way, instead of defining the function again, the program is saving the value of whatever A[i,j] is each time.

To visualize this in a grid, the function Grid should do the job.

Grid[Table[Table[A[i,j],{i,1,10}],{j,1,10}]]


in which the tens can be changed to any number. And you can also add the optional

...Frame -> All]


to the Grid function to make it nicer.

• Please, use the comment section just below the answer/question you are trying to respond to. This is strictly answer-only box. – Sektor Aug 3 '15 at 21:06
• sorry, first time – Francseco Insulla Aug 3 '15 at 21:17
• This is the recursive formula I try to solve, not including all stopping conditions. I have plenty stopping condition, as you can see with all the if's above. A_{m,k} =\left\{ \begin{array}{ll} F_m, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ m\leq k,\cr %\cr \sum_{j=m-k}^{m-2} A_{j,k},\ \ \ \ \ \ \ \ m>k. \cr – shi Aug 3 '15 at 21:17
• @FrancsecoInsulla No worries :) – Sektor Aug 3 '15 at 21:19
• I fixed it, do you want me to put the code? I'll explain too – Francseco Insulla Aug 3 '15 at 21:23