0
$\begingroup$

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[2], but it does not work that way. Any suggestions ?

$\endgroup$
6
  • 2
    $\begingroup$ Real programmers don't use loops $\endgroup$ Aug 3, 2015 at 19:03
  • 1
    $\begingroup$ 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! $\endgroup$ Aug 3, 2015 at 19:03
  • 1
    $\begingroup$ 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. $\endgroup$ Aug 3, 2015 at 19:21
  • $\begingroup$ 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) $\endgroup$
    – shi
    Aug 3, 2015 at 20:47
  • $\begingroup$ @shi So you may post your formula ... $\endgroup$ Aug 3, 2015 at 20:50

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
17
  • $\begingroup$ Please, use the comment section just below the answer/question you are trying to respond to. This is strictly answer-only box. $\endgroup$
    – Sektor
    Aug 3, 2015 at 21:06
  • $\begingroup$ sorry, first time $\endgroup$ Aug 3, 2015 at 21:17
  • $\begingroup$ 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 $\endgroup$
    – shi
    Aug 3, 2015 at 21:17
  • $\begingroup$ @FrancsecoInsulla No worries :) $\endgroup$
    – Sektor
    Aug 3, 2015 at 21:19
  • $\begingroup$ I fixed it, do you want me to put the code? I'll explain too $\endgroup$ Aug 3, 2015 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.